Skip to content

Commit e76aee8

Browse files
committed
feat: add 019
1 parent ad1e22f commit e76aee8

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
|9|[Palindrome Number][009]|Math|
1313
|13|[Roman to Integer][013]|Math, String|
1414
|14|[Longest Common Prefix][014]|String|
15+
|19|[Remove Nth Node From End of List][019]|Linked List, Two Pointers|
1516

1617

1718
## Medium
@@ -38,3 +39,4 @@
3839
[009]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/009/README.md
3940
[013]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/013/README.md
4041
[014]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/014/README.md
42+
[019]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/019/README.md

note/019/README.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
19+
Try to do this in one pass.
20+
21+
**Tags:** Linked List, Two Pointers
22+
23+
## 思路
24+
25+
题意是让你删除链表中的倒数第n个数,我的解法是利用双指针,这两个指针相差n个元素,当后面的指针扫到链表末尾的时候,自然它前面的那个指针所指向的下一个元素就是要删除的元素,即`pre.next = pre.next.next;`,但是如果一开始后面的指针指向的为空,此时代表的意思就是要删除第一个元素,即`head = head.next;`
26+
27+
``` java
28+
/**
29+
* Definition for singly-linked list.
30+
* public class ListNode {
31+
* int val;
32+
* ListNode next;
33+
* ListNode(int x) { val = x; }
34+
* }
35+
*/
36+
public class Solution {
37+
public ListNode removeNthFromEnd(ListNode head, int n) {
38+
ListNode pre = head;
39+
ListNode afterPreN = head;
40+
while (n-- != 0) {
41+
afterPreN = afterPreN.next;
42+
}
43+
if (afterPreN != null) {
44+
while (afterPreN.next != null) {
45+
pre = pre.next;
46+
afterPreN = afterPreN.next;
47+
}
48+
pre.next = pre.next.next;
49+
} else {
50+
head = head.next;
51+
}
52+
return head;
53+
}
54+
}
55+
```
56+
57+
58+
## 结语
59+
60+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
61+
62+
63+
64+
[title]: https://leetcode.com/problems/remove-nth-node-from-end-of-list
65+
[ajl]: https://github.com/Blankj/awesome-java-leetcode

project/LeetCode/leetcode/src/main/java/com/blankj/easy/_019/Solution.java

+47-1
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,62 @@
1414

1515
public class Solution {
1616

17-
class ListNode {
17+
static class ListNode {
1818
int val;
1919
ListNode next;
2020

2121
ListNode(int x) {
2222
val = x;
2323
}
24+
25+
@Override
26+
public String toString() {
27+
String str = "[" + String.valueOf(val);
28+
ListNode p = next;
29+
while (p != null) {
30+
str += ", " + String.valueOf(p.val);
31+
p = p.next;
32+
}
33+
return str + "]";
34+
}
35+
}
36+
37+
public ListNode removeNthFromEnd(ListNode head, int n) {
38+
ListNode pre = head;
39+
ListNode afterPreN = head;
40+
while (n-- != 0) {
41+
afterPreN = afterPreN.next;
42+
}
43+
if (afterPreN != null) {
44+
while (afterPreN.next != null) {
45+
pre = pre.next;
46+
afterPreN = afterPreN.next;
47+
}
48+
pre.next = pre.next.next;
49+
} else {
50+
head = head.next;
51+
}
52+
return head;
2453
}
2554

2655
public static void main(String[] args) {
2756
Solution solution = new Solution();
57+
ListNode listNode0 = new ListNode(1);
58+
ListNode listNode1 = new ListNode(2);
59+
ListNode listNode2 = new ListNode(3);
60+
ListNode listNode3 = new ListNode(4);
61+
ListNode listNode4 = new ListNode(5);
62+
listNode0.next = listNode1;
63+
listNode1.next = listNode2;
64+
listNode2.next = listNode3;
65+
listNode3.next = listNode4;
66+
listNode4.next = null;
67+
System.out.println(listNode0.toString());
68+
System.out.println(solution.removeNthFromEnd(listNode0, 2).toString());
69+
70+
ListNode listNode5 = new ListNode(1);
71+
System.out.println(listNode5.toString());
72+
ListNode res = solution.removeNthFromEnd(listNode5, 1);
73+
System.out.println(res == null ? "[]" : res.toString());
2874
}
2975
}

0 commit comments

Comments
 (0)