-
Notifications
You must be signed in to change notification settings - Fork 1
/
RemoveNthNode.java
74 lines (56 loc) · 2.24 KB
/
RemoveNthNode.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//Given the head of a linked list, remove the nth node from the end of the list and return its head.
public class RemoveNthNode {
public static void main(String[] args) {
ListNode head = new ListNode();
for(int i = 1; i < 6; i ++) {
ListNode cur = new ListNode(i);
add(cur,head);
}
ListNode ans = RemoveNthNode(head,2);
}
public static void add(ListNode cur,ListNode head) {
if(head.next == null) {
head.next = cur;
return;
}
ListNode temp = head;
while(temp.next!=null) {
temp = temp.next;
}
temp.next = cur;
}
//I'll be using a slow and fast pointer to remove the Nth node in one pass
//is there any other way to remove the nth node in one pass without slow and fast pointers?
public static ListNode RemoveNthNode(ListNode head, int n) {
ListNode slow = head, fast = head;
for(int i = 0; i < n; i++) {
fast = fast.next;
}
if(fast.next == null) return head.next; //if head is meant to be deleted, return null
//in the case where interviewer wants the data to be deleted and instead just unpointed to, make sure to set head to null
while(fast.next !=null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return head;
}
public static class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
}
//Solution: Using two pointers: slow and fast. We have fast increment to the n times ahead. If fast.next is already null, that means head is meant to be deleted
//and returned an empty list. TIP(Ask interviewer if you wanted head to be deleted or does it not matter in this scenario)
//Once fast is incremeneted n times, increment both fast and slow pointer until fast.next == null. Once done slow.next should be pointing to the node
//to be deleted. So to unchain the list and remove the pointer to that node, do slow.next = slow.next.next;
//O(n) and it is one pass.