-
Notifications
You must be signed in to change notification settings - Fork 1
/
Q22_RemoveLastKNode.java
52 lines (45 loc) · 1.08 KB
/
Q22_RemoveLastKNode.java
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
48
49
50
51
52
package jianzhi_offer;
/**
* @author weiyuwang
* @since 2019/1/29 10:13
*/
public class Q22_RemoveLastKNode {
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
/**
* LeetCode 19
* 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。
*
* 剑指offer中只是定位那个倒数第 K 个结点。
* @param head
* @param n
* @return
*/
public ListNode removeNthFromEnd(ListNode head, int n) {
if (head == null) {
return null;
}
ListNode fast = head;
// slow 设计为目标结点的前一个结点
ListNode slow = head;
for (int i = 0; i < n; i++) {
fast = fast.next;
}
if (fast == null) {
return head.next;
} else {
fast = fast.next;
}
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return head;
}
}