-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path05_RemoveNthFromEnd.java
More file actions
53 lines (44 loc) · 1.31 KB
/
05_RemoveNthFromEnd.java
File metadata and controls
53 lines (44 loc) · 1.31 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
48
49
50
51
52
53
// Problem: Remove Nth Node From End of Linked List
// Author: Ataul (codeByunique)
class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
}
}
class RemoveNthFromEnd {
public static void main(String[] args) {
// Linked List: 1 → 2 → 3 → 4 → 5
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
head.next.next.next = new ListNode(4);
head.next.next.next.next = new ListNode(5);
int n = 2;
ListNode newHead = removeNthFromEnd(head, n);
// Print updated list
while (newHead != null) {
System.out.print(newHead.val + " ");
newHead = newHead.next;
}
}
public static ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode first = dummy;
ListNode second = dummy;
// Move first n+1 steps ahead
for (int i = 0; i <= n; i++) {
first = first.next;
}
// Move both pointers
while (first != null) {
first = first.next;
second = second.next;
}
// Remove the nth node
second.next = second.next.next;
return dummy.next;
}
}