Skip to content

Commit 878171e

Browse files
committed
add java solution
1 parent 4003432 commit 878171e

File tree

1 file changed

+71
-67
lines changed

1 file changed

+71
-67
lines changed
Lines changed: 71 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,49 @@
1+
---
2+
difficulty: Easy
3+
tags:
4+
- Linked List
5+
- Two Pointers
6+
title: Remove Nth Node From End of List
7+
---
8+
19
# Remove Nth Node From End of List
210

3-
## Question
11+
## Problem
412

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
614

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/>
919

10-
Note
11-
The minimum number of nodes in list is n.
20+
### Description
1221

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.
1523

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

18-
Challenge
19-
O(n) time
20-
```
25+
#### Notice
2126

22-
## 题解
27+
The minimum number of nodes in list is *n*.
2328

24-
简单题,
25-
使用快慢指针解决此题,需要注意最后删除的是否为头节点。让快指针先走`n`步,直至快指针走到终点,找到需要删除节点之前的一个节点,改变`node->next`域即可。
29+
#### Example
2630

27-
### C++
31+
Given linked list: `1->2->3->4->5->null`, and *n* = `2`.
2832

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`.
5334

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-
}
6535

66-
if (NULL == tail) {
67-
return head->next;
68-
}
36+
#### Challenge
6937

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?
7539

76-
return head;
77-
}
78-
};
79-
```
40+
## 题解
8041

81-
以上代码单独判断了是否需要删除头节点的情况,在遇到头节点不确定的情况下,引入`dummy`节点将会使代码更加优雅,改进的代码如下
42+
简单题,使用快慢指针解决此题,需要注意最后删除的是否为头节点。让快指针先走`n`步,直至快指针走到终点,找到需要删除节点之前的一个节点,改变`node->next`域即可。见基础数据结构部分的链表解析
8243

83-
### C++ dummy node
44+
### C++
8445

85-
```c++
46+
```cpp
8647
/**
8748
* Definition of ListNode
8849
* class ListNode {
@@ -129,6 +90,49 @@ public:
12990
};
13091
```
13192

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+
132132
### 源码分析
133133

134-
引入`dummy`节点后画个图分析下就能确定`head``preDel`的转移关系了。
134+
引入`dummy`节点后画个图分析下就能确定`head``preDel`的转移关系了。 注意 while 循环中和快慢指针初始化的关系,否则容易在顺序上错一。
135+
136+
### 复杂度分析
137+
138+
极限情况下遍历两遍链表,时间复杂度为 $$O(n)$$.

0 commit comments

Comments
 (0)