LeetCode 19. Remove Nth Node From End of List

原题链接在这里:https://leetcode.com/problems/remove-nth-node-from-end-of-list/

题目:

Given a linked list, remove the nth node from the end of list and return its head.

For example,

   Given linked list: 1->2->3->4->5, and n = 2.

   After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:
Given n will always be valid.
Try to do this in one pass.

题解:

Method 1:算出总长度, 再减去n, 即为要从头多动的点。但要新要求, only one pass.

Method 2:两个指针,一个runner, 一个walker, runner先走n步,随后runner和walker一起走,直到runner指为空。

Note:1. 都是找到要去掉点的前一个点记为mark, 再通过mark.next = mark.next.next去掉对应应去掉的点.

2. 注意去掉list头点的情况,所以要建立一个dummy head. e.g. 1->2, n = 2.

Time Complexity: O(n), n是list长度. Space: O(1).

AC Java: 

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 public class Solution {
10     public ListNode removeNthFromEnd(ListNode head, int n) {
11         /*Method 1
12         if(head == null)
13             return head;
14         int len = 0;
15         ListNode temp = head;
16         while(temp != null){
17             temp = temp.next;
18             len++;
19         }
20         if(len < n){
21             return head;
22         }
23         int moveNum = len - n;
24         ListNode dunmy = new ListNode(0);
25         dunmy.next = head;
26         temp = dunmy;
27         while(moveNum > 0){
28             temp = temp.next;
29             moveNum--;
30         }
31         temp.next = temp.next.next;
32         return dunmy.next;
33         */
34         
35         //Method 2
36         if(head == null || n == 0)
37             return head;
38 
39         ListNode dummy = new ListNode(0);
40         dummy.next = head;
41         ListNode walker = dummy;
42         ListNode runner = dummy;
43         
44         while(n>0 && runner.next != null){
45             runner = runner.next;
46             n--;
47         }
48         
49         while(runner.next != null){
50             walker = walker.next;
51             runner = runner.next;
52         }
53         
54         walker.next = walker.next.next;
55         return dummy.next;
56     }
57 }

AC C++:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode() : val(0), next(nullptr) {}
 7  *     ListNode(int x) : val(x), next(nullptr) {}
 8  *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 9  * };
10  */
11 class Solution {
12 public:
13     ListNode* removeNthFromEnd(ListNode* head, int n) {
14         if(!head || n <= 0){
15             return head;
16         }
17 
18         ListNode* dummy = new ListNode(-1, head);
19         ListNode* walker = dummy;
20         ListNode* runner = dummy;
21         while(n > 0 && runner->next){
22             runner = runner->next;
23             n--;
24         }
25 
26         while(runner->next){
27             walker = walker->next;
28             runner = runner->next;
29         }
30 
31         walker->next = walker->next->next;
32         return dummy->next;
33     }
34 };

AC Python:

 1 # Definition for singly-linked list.
 2 # class ListNode:
 3 #     def __init__(self, val=0, next=None):
 4 #         self.val = val
 5 #         self.next = next
 6 class Solution:
 7     def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
 8         if not head or n <= 0:
 9             return head
10         
11         dummy = ListNode(-1, head)
12         walker = runner = dummy
13         while n > 0 and runner.next:
14             runner = runner.next
15             n -= 1
16         
17         while runner.next:
18             walker = walker.next
19             runner = runner.next
20         
21         walker.next = walker.next.next
22         return dummy.next

类似Swapping Nodes in a Linked ListDelete the Middle Node of a Linked List.

posted @ 2015-08-29 01:00  Dylan_Java_NYC  阅读(205)  评论(0编辑  收藏  举报