Skip to content

Commit 655e772

Browse files
committed
add 019
1 parent bca136c commit 655e772

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
| # | Title |
4747
| :--: | :------------------------------------------ |
4848
| 237 | [Delete Node in a Linked List][237] |
49+
| 19 | [Remove Nth Node From End of List][019] |
4950

5051

5152

@@ -83,3 +84,4 @@
8384
[038]: https://github.com/andavid/leetcode-java/blob/master/note/038/README.md
8485
[014]: https://github.com/andavid/leetcode-java/blob/master/note/014/README.md
8586
[237]: https://github.com/andavid/leetcode-java/blob/master/note/237/README.md
87+
[019]: https://github.com/andavid/leetcode-java/blob/master/note/019/README.md

note/019/README.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# [Remove Nth Node From End of List][title]
2+
3+
## Description
4+
5+
Given a linked list, remove the n<sup>th</sup> node from the end of list and return its head.
6+
7+
For example,
8+
9+
```
10+
Given linked list: 1->2->3->4->5, and n = 2.
11+
12+
After removing the second node from the end, the linked list becomes 1->2->3->5.
13+
```
14+
15+
**Note:**
16+
17+
Given n will always be valid.
18+
Try to do this in one pass.
19+
20+
## 思路
21+
22+
如果知道链表的长度,就可以从前往后一次遍历就可以了。但是求链表长度还需要遍历一次,因此总共需要两次遍历。题目要求使用一次遍历,可以使用两个指针来实现。初始都指向 head,第一个指针前进 N 步,然后两个指针同时前进直到第一个指针到达链表末尾,第二个指针后面的那个节点就是要移除的节点。
23+
24+
## [完整代码][src]
25+
26+
```java
27+
/**
28+
* Definition for singly-linked list.
29+
* public class ListNode {
30+
* int val;
31+
* ListNode next;
32+
* ListNode(int x) { val = x; }
33+
* }
34+
*/
35+
class Solution {
36+
public ListNode removeNthFromEnd(ListNode head, int n) {
37+
ListNode first = head;
38+
ListNode second = head;
39+
40+
for (int i = 1; i <= n; i++) {
41+
if (first.next == null) {
42+
return head.next;
43+
}
44+
first = first.next;
45+
}
46+
47+
while (first.next != null) {
48+
first = first.next;
49+
second = second.next;
50+
}
51+
52+
second.next = second.next.next;
53+
return head;
54+
}
55+
}
56+
```
57+
58+
[title]: https://leetcode.com/problems/remove-nth-node-from-end-of-list
59+
[src]: https://github.com/andavid/leetcode-java/blob/master/src/com/andavid/leetcode/_019/Solution.java
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Solution {
2+
3+
public static ListNode removeNthFromEnd(ListNode head, int n) {
4+
ListNode first = head;
5+
ListNode second = head;
6+
7+
for (int i = 1; i <= n; i++) {
8+
if (first.next == null) {
9+
return head.next;
10+
}
11+
first = first.next;
12+
}
13+
14+
while (first.next != null) {
15+
first = first.next;
16+
second = second.next;
17+
}
18+
19+
second.next = second.next.next;
20+
return head;
21+
}
22+
23+
public static class ListNode {
24+
int val;
25+
ListNode next;
26+
ListNode(int x) {
27+
val = x;
28+
}
29+
}
30+
31+
public static void main(String[] args) {
32+
ListNode node1 = new ListNode(1);
33+
ListNode node2 = new ListNode(2);
34+
ListNode node3 = new ListNode(3);
35+
ListNode node4 = new ListNode(4);
36+
ListNode node5 = new ListNode(5);
37+
node1.next = node2;
38+
node2.next = node3;
39+
node3.next = node4;
40+
node4.next = node5;
41+
node5.next = null;
42+
43+
ListNode node = removeNthFromEnd(node1, 2);
44+
while(node.next != null) {
45+
System.out.print(node.val + ",");
46+
node = node.next;
47+
}
48+
System.out.print(node.val);
49+
}
50+
51+
}

0 commit comments

Comments
 (0)