CTCI 2.1

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?

/* Use a HashSet to record whether a val has appeared or not. We assume that the linked list is singly linked
and the val stored is integer, this can be asked during the interview. The time complexity is O(N) and space
complexity is O(N)

In the Follow up, we could ask if the linked list could be destroy or not, if permitted, we could sort the linked
list and then pass the list one time, the time complexity could be O(NlogN) and space complexity could be O(1).
Here we assume not destroy the linked list, we use two pointer, the first pointer would pointed to a node that
might have duplicates on its right side and use the second one to iterate the right side to remove them, then 
forward the first pointer, the left side of first pointer would guarantee that there are no duplicates. The time
complexity is O(N2) and space complexity is O(1).
*/
import java.util.*;

public class RemoveDuplicateLinkList {
    public void removeDuplicate(Node head) {
        //Use a sentinel to handle null situation
        Node sent = new Node(0); sent.next = head;
        HashSet<Integer> set = new HashSet<Integer>();
        Node temp = sent;
        while(temp != null) {
            if(temp.next != null) {
                // If a value has not showed yet, record it and forward the pointer to check new value
                if(set.contains(temp.next.val) == false) {
                    set.add(temp.next.val); temp = temp.next;
                }
                else {
                    // If it has showed, remove it, but do not forward the pointer, since the value is new already
                    temp.next = temp.next.next;
                }
            }
            else {
                temp = temp.next;
            }
        }
    }

    public void removeDuplicate2(Node head) {
        Node first = head, second = head;
        while(first != null) {
            // A new node that might have depulicate, we need to pass the right side
            second = first;
            while(second != null) {
                if(second.next != null) {
                    if(second.next.val == first.val) {
                        second.next = second.next.next;
                    }
                    else {
                        second = second.next;
                    }
                }
                else {
                    second = second.next;
                }
            }
            first = first.next;
        }
    }

    public void print(Node head) {
        Node temp = head;
        while(temp != null) {
            System.out.print(temp.val + " ");
            temp = temp.next;
        }
        System.out.println("");
    }

    public static void main(String[] args) {
        Node node1 = new Node(1); Node node11 = new Node(1); Node node111 = new Node(1);
        Node node2 = new Node(2); Node node22 = new Node(2); Node node3 = new Node(3);
        node1.next = node11; node11.next = node2; node2.next = node111; node111.next = node3; node3.next = node22;
        Node head = node22;
        RemoveDuplicateLinkList rd = new RemoveDuplicateLinkList();
        rd.removeDuplicate2(head); rd.print(head);
    }
}

 

转载于:https://www.cnblogs.com/pyemma/p/3830443.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值