题解 | #删除有序链表中重复的元素-II#

删除有序链表中重复的元素-II

http://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024

思路:两种方法

1、利用map的键值对应关系,同时使用LinkedHashMap保证按照顺序执行,将链表的值作为key,将出现的次数做为value,,然后对map进行遍历,使用迭代器(注意,迭代器遍历在循环中next()方法只能使用一次,否则会出现空值问题)。

补充:忽视了这是一个有序的链表,如果出现重复,那一定时相邻排列的,所以可以得出一个简洁的解决问题的算法


/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    public ListNode deleteDuplicates (ListNode head) {
        // write code here
         ListNode listNode=new ListNode(-1);
        listNode.next=head;
        ListNode a=head;
        ListNode b=listNode;
        while (a!=null&&a.next!=null){
            if(a.val!=a.next.val){
                b=a;
            }else{
                while (a.next!=null&&a.val==a.next.val){
                    a=a.next;
                }
                b.next=a.next;
            }
            a=a.next;
        }
        return listNode.next;
    }
}
    /**
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    public ListNode deleteDuplicates (ListNode head) {
       Map<Integer,Integer>map=new LinkedHashMap<>();
        ListNode next=head;
        while (next!=null){
            if (map.get(next.val)==null){
                map.put(next.val,1);
            }else{
                map.put(next.val,map.get(next.val)+1);
            }
            next=next.next;
        }
        ListNode k=new ListNode(-1);
        ListNode l=k;
        Set<Integer> integers = map.keySet();
        Iterator<Integer>in=integers.iterator();
        while (in.hasNext()){
            //注意!in.next只能在循环中出现一次,如果多次出现,最后会出现空值,所以要保存下来
            int f=in.next();
            if (map.get(f)==1){
                ListNode listNode=new ListNode(f);
                k.next=listNode;
                k=k.next;
            }
        }
        return l.next;
    }
}

2、进行暴力破解,对每一个值后面的值进行遍历,如果出现就覆盖,然后在进行遍历,最后退出循环以后覆盖当前值


/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    public ListNode deleteDuplicates (ListNode head) {
        // write code here
        ListNode node=head;
        ListNode listNode=new ListNode(-1);
        listNode.next=head;
        ListNode a=listNode;
        ListNode b=a;
        while (node!=null){
            int num=node.val;
            ListNode h=node.next;
            ListNode d=node;
            int i=0;
            while (h!=null){
                if(h.val==num){
                    d.next=h.next;
                    i++;
                    h=d.next;
                }else{
                    h=h.next;
                    d=d.next;
                }
            }
           if(i>0){
                a.next=node.next;
                node=a.next;
            }else{
                node=node.next;
                a=a.next;
            }
        }
        return b.next;
    }
}
全部评论

相关推荐

程序员小白条:现在这个简历很没竞争力,而且很多都不要28届的,基本就看运气了,如果没简历包装的话,就海投中小厂吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务