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

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

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

import java.util.*;

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

public class Solution {
    /**
     *  
     * @param head ListNode类 
     * @return ListNode类
     */
    // 重复的全给删了,一个不留!
    public ListNode deleteDuplicates (ListNode head) {
        if(head == null || head.next == null) return head;
        ListNode fake_head = new ListNode(-1);
        fake_head.next = head;
        ListNode pre = fake_head,cur = head,next = head.next;
        
        while(next != null){
            if(cur.val == next.val){
                while(cur != null && next != null && cur.val == next.val){
                    cur = next;
                    next = next.next;
                } // 跳过重复元素!
                pre.next = next;
            }else {
                 pre = pre.next;
            }
          
           if(next != null) {
                cur = next;
                next = next.next;
           }
        }
        return fake_head.next;
    }
}
全部评论

相关推荐

09-29 16:59
已编辑
门头沟学院 Java
牛客96609213...:疯狂背刺,之前还明确设置截止日期,还有笔试,现在一帮人卡在复筛,他反而一边开启扩招,还给扩招的免笔试,真服了,你好歹先把复筛中的给处理了再说
投递大疆等公司10个岗位
点赞 评论 收藏
分享
10-10 01:10
已编辑
深圳大学 测试开发
面了100年面试不知...:六月到九月,四个项目一个实习,是魔丸吗
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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