-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveNthNode.java
More file actions
executable file
·47 lines (44 loc) · 1.41 KB
/
Copy pathRemoveNthNode.java
File metadata and controls
executable file
·47 lines (44 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package list;
/**
* @Author: Mr.Z
* @DateTime: 2021/02/22 15:29
* @Description: 19. 删除链表的倒数第 N 个结点
* https://leetcode.com/problems/remove-nth-node-from-end-of-list/
* 给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
*/
public class RemoveNthNode {
public ListNode removeNthFromEnd1(ListNode head, int n) {
// "快慢指针"
// 1,2,3,4,5,null => 1,2,3,4,5,null n = 2
// p q p q
// 1,2,null => 1,2,null n = 1
// p q p q
ListNode p = head, q = head;
while (n-- > 0)
q = q.next;
if (q == null)
return q.next;// ?
while (q.next != null) {
q = q.next;
p = p.next;
}
// 1,2,3,5,null
p.next = p.next.next;
return head;
}
public ListNode removeNthFromEnd(ListNode head, int n) {
// s ,1,2,3,4,5,null => s,1,2,3,4,5,null => s,1,2,3,4,5,null n = 2
// p/q q p q p q
ListNode start = new ListNode(0);
start.next = head;
ListNode slow = start, fast = start;
for (int i = 1; i <= n + 1; i++)
fast = fast.next;
while (fast != null) {
slow = slow.next;
fast = fast.next;
}
slow.next = slow.next.next;
return start.next;
}
}