File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
src/com/fghpdf/RemoveNthNodeFromEndOfList Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments