LeedCode---【82】【删除排序链表的重复元素II】

一、原题要求:

给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。

示例 1:

输入: 1->2->3->3->4->4->5
输出: 1->2->5

示例 2:

输入: 1->1->1->2->3
输出: 2->3

解题思路:

 h -> 1 -> 2 -> 3 -> 3 -> 4 -> 4 -> 5
pre   cur

我们始终要让pre在cur的前面,通过判断cur.val == cur.next.val判断重复元素是否存在。
h -> 1 -> 2    3    3    4 -> 4 -> 5
           |              |
           ----------------
          pre            cur
如果存在重复元素的话,我们pre.next=cur.next;cur=cur.next,如果不存在重复元素,我们pre=cur;cur=cur.next,所以这里我们要通过一个变量标记是否存在重复元素。

二、代码实现:

/**
 * @ClassName: Main
 * @Author: Mr.Ye
 * @Data: 2018-11-20 22:53
 **/

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode old = new ListNode(0);
        old.next = head;
        ListNode pre = old;
        ListNode cur = head;
        while(cur != null){
            int flag = 0;
            while(cur.next != null && cur.val == cur.next.val){
                cur = cur.next;
                flag = 1;
            }
            if(flag == 0){
                pre = cur;
            }else{
                pre.next = cur.next;
            }
            cur = cur.next;
        }
        return old.next;
    }
}

public class Main {
    public static void main(String[] args) {
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(3);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(2);
        ListNode n5 = new ListNode(5);
        ListNode n6 = new ListNode(2);
        ListNode n7 = new ListNode(2);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        n5.next = n6;
        n6.next = n7;
        System.out.println("初始链表:");
        print(n1);

        Solution solution = new Solution();
        System.out.println("最终链表:");
        print(solution.deleteDuplicates(n1));
    }

    public static void print(ListNode head) {
        for (ListNode temp = head; temp != null; temp = temp.next) {
            System.out.print(temp.val + "->");
        }
        System.out.println("null");
    }
}

 

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值