删除排序链表中的重复元素(去重和删除)java版本

该博客讲解如何使用Java实现删除排序链表中重复的数字,仅保留每个数字的单例。通过两种方法对比,展示了删除重复节点并保持链表有序的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

示例1:

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

示例 2:

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

代码

package com.star.exam;

public class Num83 {
    public static void main(String[] args) {
        int[] arr = new int[]{1, 1, 1, 2, 3};
        //删除链表中的重复元素,保留一个
        ListNode head = new ListNode(arr);
        head = deleteDuplicates1(head);
        // System.out.println(head.toString());
        while (head!=null){
            System.out.println(head.val);
            head=head.next;
        }
    }
 
    public static ListNode deleteDuplicates(ListNode head) {
        //设置一个指向当前元素的指针
        ListNode current = head;
        //当链表为空或者链表中遍历到最后一个元素时,出循环
        while (current != null && current.next != null) {
            //如果找到当前节点和它的下一个结点的值相同,删除掉下一个结点
            if (current.next.val == current.val) {
                current.next = current.next.next;
            } else {
                //如果没有找到,当前节点后移
                current = current.next;
            }
        }
        return head;
    }
 
 
    public static ListNode deleteDuplicates1(ListNode head) {
        //添加一个虚拟的表头结点
        ListNode tempHead = new ListNode(0);
        tempHead.next = head;
        //pre之前虚拟头结点,记录元素的前一个位置,如果要删除当前元素,需要前一个位置的指针
        ListNode cur = head, pre = tempHead;
        //计数器的初始值置为0
        int count = 0;
        //当前链表为空或者遍历到链表的最后一个元素时
        while (cur != null && cur.next != null) {
            //如果当前节点和下一个结点相等,删除下一个结点,计数值加1
            if (cur.val == cur.next.val) {
                cur.next = cur.next.next;
                count++;
            } else {
                //不相等的情况下需要判断计数值是否为0来确定是否需要删除当前节点
                if (count > 0) {
                    pre.next = cur.next;
                    count = 0;
                } else {
                    pre = cur;
                }
                cur = cur.next;
            }
        }
        //判断尾节点是否需要删除
        if (count > 0) {
            pre.next = cur.next;
        }
        //返回去除虚拟头结点的链表
        return tempHead.next;
    }
 
 
    static class ListNode {
        int val;
        ListNode next;
 
        ListNode(int x) {
            val = x;
        }
 
        public ListNode(int[] arr) {
            if (arr == null || arr.length == 0)
                throw new IllegalArgumentException("arr can to be empty");
            this.val = arr[0];
            ListNode cur = this;
            for (int i = 1; i < arr.length; i++) {
                cur.next = new ListNode(arr[i]);
                cur = cur.next;
            }
        }
 
        @Override
        public String toString() {
            StringBuilder res = new StringBuilder();
            ListNode cur = this;
            while (cur != null) {
                res.append(cur.val + "->");
                cur = cur.next;
            }
            res.append("NULL");
            return res.toString();
        }
    }
}

参考:

https://coordinate.blog.youkuaiyun.com/article/details/80668036

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值