Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5cba5db

Browse files
author
Ram swaroop
committed
remove duplicates in sorted linked list done + code refactoring
1 parent e984055 commit 5cba5db

File tree

6 files changed

+93
-24
lines changed

6 files changed

+93
-24
lines changed

src/me/ramswaroop/linkedlists/CloneWithRandPointers.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,17 @@
1313
*/
1414
public class CloneWithRandPointers<E extends Comparable<E>> extends DoubleLinkedList<E> {
1515

16-
public static <E extends Comparable<E>> DoubleLinkedList<E> clone(DoubleLinkedNode<E> node) {
17-
DoubleLinkedNode<E> curr = node;
16+
/**
17+
* Clones a linked list with next pointer pointing to the
18+
* next node and prev pointer pointing to any random node.
19+
*
20+
* @param list
21+
* @param <E>
22+
* @return
23+
*/
24+
public static <E extends Comparable<E>> DoubleLinkedList<E> clone(DoubleLinkedList<E> list) {
25+
DoubleLinkedNode<E> firstNode = list.getNode(0);
26+
DoubleLinkedNode<E> curr = firstNode;
1827

1928
// copy each node and insert after it
2029
while (curr != null) {
@@ -24,16 +33,16 @@ public static <E extends Comparable<E>> DoubleLinkedList<E> clone(DoubleLinkedNo
2433
}
2534

2635
// copy all random pointers from original node to the copied node
27-
curr = node;
36+
curr = firstNode;
2837
while (curr != null && curr.next != null) {
2938
curr.next.prev = (curr.prev != null) ? curr.prev.next : null;
3039
curr = curr.next.next;
3140
}
3241

3342
// separate the copied nodes into a different linked list
34-
curr = node;
35-
DoubleLinkedNode<E> cloneHead = node.next;
36-
DoubleLinkedNode<E> dupNode = cloneHead;
43+
curr = firstNode;
44+
DoubleLinkedNode<E> cloneHead = firstNode.next;
45+
DoubleLinkedNode<E> dupNode;
3746
while (curr != null && curr.next != null) {
3847
dupNode = curr.next;
3948
curr.next = (dupNode != null) ? dupNode.next : null;
@@ -54,11 +63,16 @@ public static void main(String a[]) {
5463
linkedList.getNode(1).prev = linkedList.getNode(2);
5564
linkedList.getNode(2).prev = linkedList.getNode(0);
5665
linkedList.getNode(3).prev = linkedList.getNode(1);
66+
System.out.println("======Original======");
5767
linkedList.printList();
58-
DoubleLinkedList<Integer> clonedList = clone(linkedList.getNode(0));
68+
DoubleLinkedList<Integer> clonedList = clone(linkedList);
69+
System.out.println("======Cloned======");
70+
clonedList.printList();
71+
System.out.println("======Cloned (Modified)======");
5972
clonedList.set(0, 234);
6073
clonedList.set(1, 567);
6174
clonedList.printList();
75+
System.out.println("======Original======");
6276
linkedList.printList();
6377
}
6478
}

src/me/ramswaroop/linkedlists/DetectLoop.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ public class DetectLoop<E extends Comparable<E>> extends SingleLinkedList<E> {
2727
* Time: O(n)
2828
* Space: O(1)
2929
*
30-
* @param node
30+
* @param list
3131
* @return
3232
*/
33-
public static <E extends Comparable<E>> boolean isLoopPresent(SingleLinkedNode<E> node) {
34-
SingleLinkedNode<E> prev = node, curr = node;
33+
public static <E extends Comparable<E>> boolean isLoopPresent(SingleLinkedList<E> list) {
34+
SingleLinkedNode<E> firstNode = list.getNode(0);
35+
SingleLinkedNode<E> prev = firstNode, curr = firstNode;
3536
while (curr != null && curr.next != null) {
3637
prev = prev.next;
3738
curr = curr.next.next;
@@ -73,6 +74,6 @@ public static void main(String a[]) {
7374
linkedList.add(44);
7475
linkedList.add(55);
7576
linkedList.getNode(4).next = linkedList.getNode(3);
76-
System.out.println(isLoopPresent(linkedList.getNode(0)));
77+
System.out.println(isLoopPresent(linkedList));
7778
}
7879
}

src/me/ramswaroop/linkedlists/IsPalindrome.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@ public class IsPalindrome<E extends Comparable<E>> extends SingleLinkedList<E> {
1818
* Uses Stack to test whether a linked list starting
1919
* from {@param node} is palindrome or not.
2020
*
21-
* @param node
21+
* @param list
2222
* @return
2323
*/
24-
public static <E extends Comparable<E>> boolean isPalindrome(SingleLinkedNode<E> node) {
25-
SingleLinkedNode<E> curr = node;
24+
public static <E extends Comparable<E>> boolean isPalindrome(SingleLinkedList<E> list) {
25+
SingleLinkedNode<E> head = list.getNode(0);
26+
SingleLinkedNode<E> curr = head;
2627
Stack<SingleLinkedNode<E>> stack = new LinkedStack<>();
2728

2829
while (curr != null) {
2930
stack.push(curr);
3031
curr = curr.next;
3132
}
3233

33-
curr = node;
34+
curr = head;
3435

3536
while (curr != null) {
3637
if (curr.item != stack.pop().item) {
@@ -72,7 +73,7 @@ public static void main(String a[]) {
7273
linkedList.add(11);
7374
linkedList.add(00);
7475
linkedList.printList();
75-
System.out.println(isPalindrome(linkedList.getNode(0)));
76+
System.out.println(isPalindrome(linkedList));
7677
linkedList.printList();
7778
System.out.println(linkedList.isPalindromeRecursive(linkedList.getNode(0)));
7879
}

src/me/ramswaroop/linkedlists/MiddleNode.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
*/
1313
public class MiddleNode<E extends Comparable<E>> extends SingleLinkedList<E> {
1414

15-
public static <E extends Comparable<E>> SingleLinkedNode<E> getMiddleNode(SingleLinkedNode<E> node) {
16-
SingleLinkedNode<E> slow = node;
17-
SingleLinkedNode<E> fast = node;
15+
public static <E extends Comparable<E>> SingleLinkedNode<E> getMiddleNode(SingleLinkedList<E> list) {
16+
SingleLinkedNode<E> slow = list.getNode(0);
17+
SingleLinkedNode<E> fast = list.getNode(0);
1818
while (fast != null && fast.next != null) {
1919
slow = slow.next;
2020
fast = fast.next.next;
@@ -33,6 +33,6 @@ public static void main(String a[]) {
3333
linkedList.add(66);
3434
linkedList.add(77);
3535
linkedList.add(88);
36-
System.out.println(getMiddleNode(linkedList.getNode(0)).item);
36+
System.out.println(getMiddleNode(linkedList).item);
3737
}
3838
}

src/me/ramswaroop/linkedlists/NthNodeFromLast.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
*/
1313
public class NthNodeFromLast<E extends Comparable<E>> extends SingleLinkedList<E> {
1414

15-
public static <E extends Comparable<E>> SingleLinkedNode<E> getNthNodeFromLast(SingleLinkedNode<E> node, int n) {
16-
SingleLinkedNode<E> slow = node;
17-
SingleLinkedNode<E> fast = node;
15+
public static <E extends Comparable<E>> SingleLinkedNode<E> getNthNodeFromLast(SingleLinkedList<E> list, int n) {
16+
SingleLinkedNode<E> slow = list.getNode(0);
17+
SingleLinkedNode<E> fast = list.getNode(0);
1818
// move the fast reference ahead of slow reference by 'n' nodes
1919
for (int i = 0; i < n; i++) {
2020
// assert length of linkedlist > n
@@ -38,6 +38,6 @@ public static void main(String a[]) {
3838
linkedList.add(66);
3939
linkedList.add(77);
4040
linkedList.add(88);
41-
System.out.println(getNthNodeFromLast(linkedList.getNode(0), 3).item);
41+
System.out.println(getNthNodeFromLast(linkedList, 3).item);
4242
}
4343
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package me.ramswaroop.linkedlists;
2+
3+
import me.ramswaroop.common.SingleLinkedList;
4+
import me.ramswaroop.common.SingleLinkedNode;
5+
6+
/**
7+
* Created by IntelliJ IDEA.
8+
*
9+
* @author: ramswaroop
10+
* @date: 6/18/15
11+
* @time: 2:35 PM
12+
*/
13+
public class RemoveDuplicates<E extends Comparable<E>> extends SingleLinkedList<E> {
14+
15+
/**
16+
* Removes duplicates in a sorted linked list
17+
* by traversing it once.
18+
*
19+
* @param list
20+
* @param <E>
21+
*/
22+
public static <E extends Comparable<E>> void removeDuplicates(SingleLinkedList<E> list) {
23+
SingleLinkedNode<E> firstNode = list.getNode(0), curr = firstNode;
24+
int index = 0;
25+
while (curr != null) {
26+
while (curr.next != null && curr.item == curr.next.item) {
27+
list.remove(index + 1);
28+
}
29+
index++;
30+
curr = curr.next;
31+
}
32+
}
33+
34+
public static void main(String a[]) {
35+
SingleLinkedList<Integer> linkedList = new SingleLinkedList<>();
36+
linkedList.add(00);
37+
linkedList.add(00);
38+
linkedList.add(00);
39+
linkedList.add(11);
40+
linkedList.add(11);
41+
linkedList.add(22);
42+
linkedList.add(22);
43+
linkedList.add(22);
44+
linkedList.add(22);
45+
linkedList.add(33);
46+
linkedList.add(33);
47+
linkedList.add(33);
48+
linkedList.add(33);
49+
linkedList.printList();
50+
removeDuplicates(linkedList);
51+
linkedList.printList();
52+
}
53+
}

0 commit comments

Comments
 (0)