LeetCode019 Remove Nth Node From End of List

详细见:leetcode.com/problems/remove-nth-node-from-end-of-list


Java Solution: github

package leetcode;

public class P019_RemoveNthFromEndOfList {
	
	/*
	 * 	1ms
	 * 	5.49%
	 */
	static class Solution {
		public ListNode removeNthFromEnd(ListNode head, int n) {
			ListNode cur = head, pre = head;
			int count = 0;
			while (cur != null) {
				cur = cur.next;
				++ count;
				if (count == n) {
					break;
				}
			}
			if (cur == null && count == n)
				return head.next;
			if (count != n)
				return head;
			while (cur.next != null) {
				pre = pre.next;
				cur = cur.next;
			}
			pre.next = pre.next.next;
		    return head;
		}
	}
	static class ListNode {
		int val;
		ListNode next;
		public ListNode(int val) {
			this.val = val;
		}
	}
}

C Solution: github

/*
    url: leetcode.com/problems/remove-nth-node-from-end-of-list/
    6ms 19.31%
*/

#include <stdlib.h>
#include <stdio.h>

struct ListNode {
    int val;
    struct ListNode *next;
};

//all head and n is valid
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
    struct ListNode * front = head, * tail = head;
    int i = 0;
    for (i = 0; i < n; i ++) {
        front = front->next;
    }
    if (front == NULL) return head->next;
    front = front->next;
    while (front != NULL) {
        front = front->next;
        tail = tail->next;
    }
    tail->next = tail->next->next;
    return head;
}

struct ListNode * convert_int_to_ListNode(int * arr, int n) {
    struct ListNode * head = NULL;
    struct ListNode * travel = NULL;
    struct ListNode * temp = NULL;
    int i = 0;
    if (n == 0 || n < 0) return NULL;
    travel = (struct ListNode *) malloc(sizeof(struct ListNode));
    travel->val = *(arr + 0);
    travel->next = NULL;
    head = travel;
    for (i = 1; i < n; i ++) {
        temp = (struct ListNode *) malloc(sizeof(struct ListNode));
        temp->val = *(arr + i);
        temp->next = NULL;
        travel->next = temp;
        travel = travel->next;
    }
    return head;
}

void free_ListNode(struct ListNode * l) {
    struct ListNode * l1 = l, * l2 = l;
    if (l == NULL) return;
    while (l1 != NULL) {
        l2 = l2->next;
        free(l1);
        l1 = l2;
    }
}

void print_ListNode(struct ListNode * h) {
    while (h != NULL) {
        printf("%d ", h->val);
        h = h->next;
    }
    printf("\r\n");
}

int main() {
    int a[] = {1, 2, 3, 4, 5};
    struct ListNode * head = convert_int_to_ListNode(a, 5);
    struct ListNode * answer = removeNthFromEnd(head, 2);
    print_ListNode(answer);
}

Python Solution: github

#coding=utf-8
'''
    url: leetcode.com/problems/remove-nth-node-from-end-of-list/
    @author:     zxwtry
    @email:      zxwtry@qq.com
    @date:       2017年3月29日
    @details:    Solution: 75ms 10.07%
'''

from leetcode.Utils import *

class Solution(object):
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        f, t = head, head
        for i in range(n):
            if f == None:
                return head
            else:
                f = f.next
        if f == None:
            return head.next
        while f.next != None:
            t = t.next
            f = f.next
        t.next = None if t.next == None else t.next.next
        return head 

if __name__ == "__main__":
    head = convertArrayToListNode([1, 2, 3, 4, 5, 6])
    ans = Solution().removeNthFromEnd(head, 3)
    printListNode(ans)
    




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值