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

链表去重

时间:2015-11-07 23:21:01      阅读:488      评论:0      收藏:0      [点我收藏+]

标签:

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?

 

题解如下:

解决此题关键是了解java reference及链表基本知识

 

链表类:

public class LinkedListNode
    {
        LinkedListNode next=null;
        int data;
        public LinkedListNode(int d) {
            data=d;
        }
        
        public void appendToTail(int d)
        {
            LinkedListNode end= new LinkedListNode(d);
            LinkedListNode node=this;
            while(node.next!=null)
            {
                node=node.next;
            }
            node.next=end;
        }    
        
        public static void printList(LinkedListNode node) {
            LinkedListNode tempnode=node;
            
        }
        
        public static LinkedListNode deleteNode(LinkedListNode head,int d)
            {
                LinkedListNode node=head;
                if(node.data==d) return node.next;
                while(node.next!=null)
                {
                    if(node.next.data==d)
                    {
                        node.next=node.next.next;
                        return head;
                    }
                    node=node.next;
                }
                return null;
            }
    }

问题解法:

import java.util.HashSet;

public class RemoveDupsFromList {
    
    public static void deleteDups(LinkedListNode n) {
        HashSet<Integer>set=new HashSet<>();
        LinkedListNode previous=null;
        while (n!=null) {
            if(set.contains(n.data)) previous.next=n.next;
            else
            {
                set.add(n.data);
                previous=n;
            }
            n=n.next;
        }
    }

    //without buffer
    public static void deleteDups2(LinkedListNode n) {
        if(n==null) return;
        LinkedListNode head,pre,cur,tmp;
        head=pre=cur=n;
        while (cur!=null) {
            tmp=head;
            while (tmp!=cur) {
                if(cur.data==tmp.data)
                {
                    pre.next=cur.next;
                    cur=cur.next;
                    break;
                }
                tmp=tmp.next;
            }
            if(tmp==cur)
            {
                pre=cur;
                cur=cur.next;
            }
        }
        
    }
    
    
    public static void deleteDups3(LinkedListNode n) {
        
        
    }
    
    
    

    
    
    public static void main(String[] args) {
        LinkedListNode node=new LinkedListNode(5);
        node.appendToTail(4);
        node.appendToTail(3);
        node.appendToTail(3);
        node.appendToTail(4);
        node.appendToTail(2);
        node.appendToTail(2);
        
        LinkedListNode node1=node;
    
        
        deleteDups2(node);
        System.out.println(node.data);

        
    }
    
    public static void test(LinkedListNode node) {
        node=node.next;
        System.out.println(node.data);
    }

}

 

链表去重

标签:

原文地址:http://www.cnblogs.com/AlexWei/p/4946219.html

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