Remove Duplicates from Unsorted List
约 1 分钟· 本文阅读 次
本页目录
Remove Duplicates from Unsorted List
Write code to remove duplicates from an unsorted linked list
FOLLOW UP
How would you solve this problem if a temporary buffer is not allowed?
Solution
根据题意进行删除即可
Complexity
时间复杂度 O(n),空间复杂度 O(n)
Code
public static Node removeDups(Node head){
HashSet<Integer> intSet = new HashSet<Integer>();
Node cur = head;
Node previous = null;
while (cur != null){
if (intSet.contains(cur.data)){
previous.next = cur.next;
}
else{
intSet.add(cur.data);
previous = cur;
}
cur = cur.next;
}
return head;
}
/**
* Iterate with two pointer: one iterates through the linked list, and the
* other checks all subsequent nodes for duplicates
*/
public static Node removeDupsNoExtraSpace(Node head){
Node cur = head;
while (cur != null){
Node pointer = cur;
while (pointer.next != null){
if (pointer.next.data == cur.data){
pointer.next = pointer.next.next;
}
else {
pointer = pointer.next;
}
}
cur = cur.next;
}
return head;
}相关文章
- Add Two Numbers2016-01-07
- Check Cycle2016-01-07
- Check Intersection2016-01-06
- Palindrome Linked List2016-01-06
已恢复到上次阅读位置