-
-
Save kelly-us/2aa0e810fa70696fb056 to your computer and use it in GitHub Desktop.
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
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