Leetcode 82. Remove Duplicates from Sorted List II

15 篇文章 0 订阅
9 篇文章 0 订阅

题目描述

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

Example 1:

Input: 1->2->3->3->4->4->5
Output: 1->2->5
Example 2:

Input: 1->1->1->2->3
Output: 2->3
(删除链表中多次出现的元素),其中一次都不能包含

思路

有两种情况,一种是xian
1. 如果前后相同,即有一段元素的,一直循环找到相同序列的最后一个元素。在这里使用一个标记位如果经过这一个步骤处理相同元素
2. 处理相同元素序列
标记为真的话,说明经过了步骤一中的遍历相同元素,那么jiu yao
pre指向相同序列的前驱,cur指向相同序列的最后一个元素,
获取到 cur的下面一个元素,
pre.next = cur.next;
cur = pre.next

比如上面的 pre =1 cur =2 , 然后cur走到相同序列的最后一个只
上面有句话就把一个相同序列删除完

  1. pre和cur两个结点的值不一样的话,两个往下遍历,什么也不做
    #代码(Java)

public class RemoveDuplicatesfromSortedList11 {
    /**
     * 从有序链表中删除相同元素的元素,一个都不留
     * 1-2-2-3-3-4-5
     * 1-4-5
     * @param head
     * @return
     */
     public  static ListNode solution(ListNode head){
        if(head==null|| head.next==null) {
            return head;
        }
        ListNode dump = new ListNode(0);
        dump.next =  head;
        boolean mark  = false;
        ListNode current =  head, pre  = dump;
        while(current!=null && current.next!=null){
            //碰到有一系列相同的元素,找到最后一个相似的元素
            while(current!=null && current.next!=null && current.val==current.next.val){
                mark = true;
                current=current.next;
            }
            //清楚掉没用的元素
            if(mark){
                 pre.next =current.next;
                current = pre.next;
                mark = false;
            }
            //不需要作出任何改变,不需要删除任何元素,正常情况下的
            if( current!=null && current.next!=null && current.val!=current.next.val){
                pre = current;
                current = current.next;
            }
        }
        return dump.next;
    }

    public static void main(String[] args) {
        ListNode head= new ListNode(5);
        ListNode cur = head;
        int [] test ={1,1,2,2,4,5,5,6,6};

        for(int i=0;i<test.length;++i){
            ListNode new_node =new ListNode(test[i]);
            cur.next = new_node;
            cur = new_node;
        }
        RemoveDuplicatesfromSortedList11.solution(head);
    }
}

优化代码

意思基本上是一样的,只不过通过pre.next 是否等于cur判断是否又相同的序列, 如果有相同的序列的话,pre和cur肯定不是相连的,在这种情况就要删除,改变pre 的后继结点即可,然后cur继续遍历下一个结点

public  static ListNode solution_2(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode dump = new ListNode(0);
        dump.next = head;
        ListNode cur = head, pre = dump;
        while(cur!=null){
            while(cur.next!=null && cur.val == cur.next.val)
                cur = cur.next;
                //证明
            if(pre.next ==cur){
                pre = pre.next;
            }
            else{
                pre.next = cur.next;

            }
            cur =cur.next;
        }
        return dump.next;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值