Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given
Given
Given
1->1->2
, return 1->2
.Given
1->1->2->3->3
, return 1->2->3
.
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 leetcode.linkedlist; | |
import java.util.Hashtable; | |
import javax.swing.text.html.HTMLDocument.RunElement; | |
/** | |
* Solution: One pointer - Solution 3, Two pointer(with runner) - Solution 1 (same to 3, only use runner replace current.next) | |
* | |
* @author jeffwan | |
* @date Mar 7, 2014 | |
*/ | |
public class DeleteDuplicates { | |
/** | |
* Solution1:Two pointer, Iterative way. two while loops. one current, one runner. still O(n) | |
* Remember to return head but not current, current already go to last node. | |
* @param head | |
* @return | |
*/ | |
public ListNode deleteDuplicates(ListNode head) { | |
if (head == null) { | |
return null; | |
} | |
ListNode current = head; | |
ListNode runner = head.next; | |
while(runner != null) { | |
if (runner.val == current.val) { | |
current.next = runner.next; | |
} else { | |
current = runner; | |
} | |
runner = runner.next; | |
} | |
return head; | |
} | |
/** | |
* Solution2: Single Pointer | |
* This way is almost same to remove duplicates from sorted List II(with dummy node). | |
* Only difference is we start from current, use current.next = current.next.next to skip current.next | |
* | |
* Actually, for this problem, solution1 seems more straightforward. | |
*/ | |
public ListNode deleteDuplicates2(ListNode head) { | |
if (head == null || head.next == null) { | |
return head; | |
} | |
ListNode current = head; | |
while (current!= null && current.next != null) { | |
if(current.val == current.next.val) { | |
int val = current.val; | |
while(current.next != null && current.next.val == val) { | |
current.next = current.next.next; | |
} | |
} | |
current = current.next; | |
} | |
return head; | |
} | |
/** | |
* Solution 3: Single Pointer -- Best solution I think. | |
* Actually, we could use this ways directly but not solution2. Because we don't need to remember to target value. | |
* Solution 4 is useless, we just need to campare with head.val, no need to use a map. | |
* @param head | |
* @return | |
*/ | |
public ListNode deleteDuplicates3(ListNode head) { | |
if (head == null || head.next == null) { | |
return head; | |
} | |
ListNode newHead = head; | |
while (head.next != null) { | |
if (head.next.val == head.val) { | |
head.next = head.next.next; | |
} else { | |
head = head.next; | |
} | |
} | |
return newHead; | |
} | |
/** | |
* Solution4: Extra Space - Hashtable -- complier error in LeetCode Online Judge but really works. | |
* @param head | |
* @return | |
*/ | |
public ListNode deleteDuplicates4(ListNode head) { | |
if(head == null) { | |
return null; | |
} | |
ListNode current = head; | |
Hashtable table = new Hashtable(); | |
table.put(current.val, true); | |
while (current.next != null) { | |
if (table.containsKey(current.next)) { | |
current.next = current.next.next; | |
} else { | |
table.put(current.next.val, true); | |
current = current.next; | |
} | |
} | |
return current; | |
} | |
class ListNode { | |
int val; | |
ListNode next; | |
ListNode (int x) { | |
this.val = x; | |
} | |
} | |
} |
No comments:
Post a Comment