Skip to content

Commit 54f09be

Browse files
committed
feat: solved 19
1 parent fadd744 commit 54f09be

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.fghpdf.RemoveNthNodeFromEndOfList;
2+
3+
import com.fghpdf.ListNode;
4+
5+
/**
6+
* @author fghpdf
7+
* @date 2019/11/15
8+
* https://leetcode.com/problems/remove-nth-node-from-end-of-list/
9+
* fast and slow pointer
10+
* so easy!
11+
**/
12+
public class Solution {
13+
public ListNode removeNthFromEnd(ListNode head, int n) {
14+
ListNode fast = head;
15+
ListNode slow = head;
16+
17+
if (head == null) {
18+
return head;
19+
}
20+
21+
while (fast != null && n != 0) {
22+
fast = fast.next;
23+
n--;
24+
}
25+
26+
if (fast == null) {
27+
return head.next;
28+
}
29+
30+
while (fast.next != null) {
31+
fast = fast.next;
32+
slow = slow.next;
33+
}
34+
35+
slow.next = slow.next.next;
36+
return head;
37+
}
38+
}

0 commit comments

Comments
 (0)