-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRemoveDuplicatesFromLinkedList.java
87 lines (66 loc) · 2.39 KB
/
RemoveDuplicatesFromLinkedList.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
75
76
77
78
79
80
81
82
83
84
85
86
87
public class RemoveDuplicatesFromLinkedList {
//Given the head of a linked list, remove the nth node from the end of the list and return its head.
public static void main(String[] args) {
ListNode head = new ListNode();
int j = 1;
for(int i = 1; i < 6; i ++) {
ListNode cur = new ListNode(i);
add(cur,head);
j++;
if(j%2 == 0) {
add(new ListNode(i),head);
}
}
for(ListNode cur = head; cur!=null; cur = cur.next) {
System.out.print(cur.val + " ");
}
System.out.println();
deleteDuplicates(head);
for(ListNode cur = head; cur.next!=null; cur = cur.next) {
System.out.print(cur.val + " ");
}
}
public static ListNode deleteDuplicates(ListNode head) {
if(head == null) return head; //if List is empty
ListNode sentinel = new ListNode(0,head); //using a sentinel node to edge case if the head of the node is a duplcate
ListNode pred = sentinel; //set pred to sentinel
while(head != null) {
if(head.next!= null && head.val == head.next.val) { //
while(head.next !=null && head.val == head.next.val) {
head = head.next;
}
pred.next = head.next;
} else {
pred = pred.next;
}
head = head.next;
}
return sentinel.next;
}
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 class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
}