-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveNthNode19.java
More file actions
34 lines (26 loc) · 942 Bytes
/
removeNthNode19.java
File metadata and controls
34 lines (26 loc) · 942 Bytes
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
public class removeNthNode19 {
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode fast = head , slow = head;
for (int i =0 ; i < n ; i++) {
fast = fast.next ;
}
if (fast == null) {
return head.next ;
}
while (fast.next != null){
fast = fast.next ;
slow = slow.next ;
}
slow.next = slow.next.next ;
return head ;
}
public static void main(String[] args) {
}
}