Last active
August 29, 2015 14:02
-
-
Save chrislukkk/fc2ce325c06ab6e47c1e to your computer and use it in GitHub Desktop.
CareerCup_2.1 - Remove Duplicate List Element
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Chapter2; | |
//Singly Linked List | |
public class MyLinkedList<T> { | |
public Node<T> head; | |
public MyLinkedList(Node<T> h) { | |
head = h; | |
} | |
public MyLinkedList(T[] dataArray) { | |
if (dataArray == null || dataArray.length <= 0) | |
return; | |
head = new Node<>(dataArray[0], null); | |
Node<T> node = head; | |
for (int i = 1; i < dataArray.length; i++) { | |
node.next = new Node<T>(dataArray[i], null); | |
node = node.next; | |
} | |
} | |
public void print() { | |
Node<T> cur = head; | |
while (cur != null) { | |
System.out.print(cur.data); | |
if (cur.next != null) { | |
System.out.print(" -> "); | |
} | |
cur = cur.next; | |
} | |
System.out.println(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Chapter2; | |
public class Node<T> { | |
public T data; | |
public Node<T> next; | |
public Node(T d, Node<T> n){ | |
data = d; | |
next = n; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Chapter2; | |
import java.util.HashSet; | |
public class RemoveDuplication { | |
public static <T> void removeDup(MyLinkedList<T> list) { | |
if (list == null || list.head == null) | |
return; | |
HashSet<T> hashSet = new HashSet<>(); | |
Node<T> prev = list.head; | |
Node<T> cur = list.head.next; | |
hashSet.add(list.head.data); | |
while (cur != null) { | |
if (hashSet.contains(cur.data)) { | |
prev.next = cur.next; | |
cur.next = null; | |
cur = prev.next; | |
} else { | |
hashSet.add(cur.data); | |
prev = cur; | |
cur = cur.next; | |
} | |
} | |
} | |
public static <T> void removeDupNoBuf(MyLinkedList<T> list) { | |
if (list == null || list.head == null) | |
return; | |
Node<T> start = list.head; | |
while (start != null) { | |
Node<T> prev = start; | |
Node<T> cur = start.next; | |
while (cur != null) { | |
if (cur.data == start.data || cur.data == prev.data) { | |
prev.next = cur.next; | |
cur.next = null; | |
cur = prev.next; | |
} else { | |
prev = cur; | |
cur = cur.next; | |
} | |
} | |
start = start.next; | |
} | |
} | |
public static void main(String[] args) { | |
MyLinkedList<Integer> list = new MyLinkedList<>(new Integer[] { 2, 7, 5, 4, 2, 2, 4, 6 }); | |
list.print(); | |
removeDup(list); | |
list.print(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment