|
| 1 | +--- |
| 2 | +difficulty: Easy |
| 3 | +tags: |
| 4 | +- Linked List |
| 5 | +- Two Pointers |
| 6 | +title: Remove Nth Node From End of List |
| 7 | +--- |
| 8 | + |
1 | 9 | # Remove Nth Node From End of List
|
2 | 10 |
|
3 |
| -## Question |
| 11 | +## Problem |
4 | 12 |
|
5 |
| -- lintcode: [(174) Remove Nth Node From End of List](http://www.lintcode.com/en/problem/remove-nth-node-from-end-of-list/) |
| 13 | +### Metadata |
6 | 14 |
|
7 |
| -``` |
8 |
| -Given a linked list, remove the nth node from the end of list and return its head. |
| 15 | +- tags: Linked List, Two Pointers |
| 16 | +- difficulty: Easy |
| 17 | +- source(lintcode): <https://www.lintcode.com/problem/remove-nth-node-from-end-of-list/> |
| 18 | +- source(leetcode): <https://leetcode.com/problems/remove-nth-node-from-end-of-list/> |
9 | 19 |
|
10 |
| -Note |
11 |
| -The minimum number of nodes in list is n. |
| 20 | +### Description |
12 | 21 |
|
13 |
| -Example |
14 |
| -Given linked list: 1->2->3->4->5->null, and n = 2. |
| 22 | +Given a linked list, remove the n<sup>th</sup> node from the end of list and return its head. |
15 | 23 |
|
16 |
| -After removing the second node from the end, the linked list becomes 1->2->3->5->null. |
17 | 24 |
|
18 |
| -Challenge |
19 |
| -O(n) time |
20 |
| -``` |
| 25 | +#### Notice |
21 | 26 |
|
22 |
| -## 题解 |
| 27 | +The minimum number of nodes in list is *n*. |
23 | 28 |
|
24 |
| -简单题, |
25 |
| -使用快慢指针解决此题,需要注意最后删除的是否为头节点。让快指针先走`n`步,直至快指针走到终点,找到需要删除节点之前的一个节点,改变`node->next`域即可。 |
| 29 | +#### Example |
26 | 30 |
|
27 |
| -### C++ |
| 31 | +Given linked list: `1->2->3->4->5->null`, and *n* = `2`. |
28 | 32 |
|
29 |
| -```c++ |
30 |
| -/** |
31 |
| - * Definition of ListNode |
32 |
| - * class ListNode { |
33 |
| - * public: |
34 |
| - * int val; |
35 |
| - * ListNode *next; |
36 |
| - * ListNode(int val) { |
37 |
| - * this->val = val; |
38 |
| - * this->next = NULL; |
39 |
| - * } |
40 |
| - * } |
41 |
| - */ |
42 |
| -class Solution { |
43 |
| -public: |
44 |
| - /** |
45 |
| - * @param head: The first node of linked list. |
46 |
| - * @param n: An integer. |
47 |
| - * @return: The head of linked list. |
48 |
| - */ |
49 |
| - ListNode *removeNthFromEnd(ListNode *head, int n) { |
50 |
| - if (NULL == head || n < 0) { |
51 |
| - return NULL; |
52 |
| - } |
| 33 | +After removing the second node from the end, the linked list becomes `1->2->3->5->null`. |
53 | 34 |
|
54 |
| - ListNode *preN = head; |
55 |
| - ListNode *tail = head; |
56 |
| - // slow fast pointer |
57 |
| - int index = 0; |
58 |
| - while (index < n) { |
59 |
| - if (NULL == tail) { |
60 |
| - return NULL; |
61 |
| - } |
62 |
| - tail = tail->next; |
63 |
| - ++index; |
64 |
| - } |
65 | 35 |
|
66 |
| - if (NULL == tail) { |
67 |
| - return head->next; |
68 |
| - } |
| 36 | +#### Challenge |
69 | 37 |
|
70 |
| - while (tail->next) { |
71 |
| - tail = tail->next; |
72 |
| - preN = preN->next; |
73 |
| - } |
74 |
| - preN->next = preN->next->next; |
| 38 | +Can you do it without getting the length of the linked list? |
75 | 39 |
|
76 |
| - return head; |
77 |
| - } |
78 |
| -}; |
79 |
| -``` |
| 40 | +## 题解 |
80 | 41 |
|
81 |
| -以上代码单独判断了是否需要删除头节点的情况,在遇到头节点不确定的情况下,引入`dummy`节点将会使代码更加优雅,改进的代码如下。 |
| 42 | +简单题,使用快慢指针解决此题,需要注意最后删除的是否为头节点。让快指针先走`n`步,直至快指针走到终点,找到需要删除节点之前的一个节点,改变`node->next`域即可。见基础数据结构部分的链表解析。 |
82 | 43 |
|
83 |
| -### C++ dummy node |
| 44 | +### C++ |
84 | 45 |
|
85 |
| -```c++ |
| 46 | +```cpp |
86 | 47 | /**
|
87 | 48 | * Definition of ListNode
|
88 | 49 | * class ListNode {
|
@@ -129,6 +90,49 @@ public:
|
129 | 90 | };
|
130 | 91 | ```
|
131 | 92 |
|
| 93 | +### Java |
| 94 | + |
| 95 | +```java |
| 96 | +/** |
| 97 | + * Definition for singly-linked list. |
| 98 | + * public class ListNode { |
| 99 | + * int val; |
| 100 | + * ListNode next; |
| 101 | + * ListNode(int x) { val = x; } |
| 102 | + * } |
| 103 | + */ |
| 104 | +class Solution { |
| 105 | + public ListNode removeNthFromEnd(ListNode head, int n) { |
| 106 | + if (head == nul) return head; |
| 107 | + |
| 108 | + ListNode dummy = new ListNode(0); |
| 109 | + dummy.next = head; |
| 110 | + ListNode fast = head; |
| 111 | + ListNode slow = dummy; |
| 112 | + for (int i = 0; i < n; i++) { |
| 113 | + fast = fast.next; |
| 114 | + } |
| 115 | + |
| 116 | + while(fast != null) { |
| 117 | + fast = fast.next; |
| 118 | + slow = slow.next; |
| 119 | + } |
| 120 | + |
| 121 | + // gc friendly |
| 122 | + // ListNode toBeDeleted = slow.next; |
| 123 | + slow.next = slow.next.next; |
| 124 | + // toBeDeleted.next = null; |
| 125 | + // toBeDeleted = null; |
| 126 | + |
| 127 | + return dummy.next; |
| 128 | + } |
| 129 | +} |
| 130 | +``` |
| 131 | + |
132 | 132 | ### 源码分析
|
133 | 133 |
|
134 |
| -引入`dummy`节点后画个图分析下就能确定`head`和`preDel`的转移关系了。 |
| 134 | +引入`dummy`节点后画个图分析下就能确定`head`和`preDel`的转移关系了。 注意 while 循环中和快慢指针初始化的关系,否则容易在顺序上错一。 |
| 135 | + |
| 136 | +### 复杂度分析 |
| 137 | + |
| 138 | +极限情况下遍历两遍链表,时间复杂度为 $$O(n)$$. |
0 commit comments