Skip to content

Instantly share code, notes, and snippets.

@kelly-us
Created April 13, 2015 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kelly-us/2aa0e810fa70696fb056 to your computer and use it in GitHub Desktop.
Save kelly-us/2aa0e810fa70696fb056 to your computer and use it in GitHub Desktop.
public void removeDuplicate(ListNode head){
if(head == null || head.next == null) return;
HashMap<Integer, Boolean> map = new HashMap<Integer, Boolean>();
ListNode cur = head;
ListNode prev = null;
while(cur != null){
if(map.get(cur.data) != true){
map.put(cur.data, true);
prev = cur;
}
else{
prev.next = cur.next;
}
cur = cur.next;
}
}
//Follow up
public void removeDuplicate_runner(ListNode head){
if(head == null || head.next == null) return;
ListNode cur = head;
while(cur != null){
ListNode runner = cur;
while(runner.next != null){
if(runner.next.data == cur.data){
runner.next = runner.next.next;
}
else{
runner = runner.next;
}
}
cur = cur.next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment