码迷,mamicode.com
首页 > 其他好文 > 详细

CTCI 2.1

时间:2014-07-08 00:59:05      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   java   color   for   

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);
    }
}

 

CTCI 2.1,布布扣,bubuko.com

CTCI 2.1

标签:des   style   blog   java   color   for   

原文地址:http://www.cnblogs.com/pyemma/p/3830443.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!