LeetCode-083-删除排序链表中的重复元素

删除排序链表中的重复元素

题目描述:存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除所有重复的元素,使每个元素 只出现一次 。

返回同样按升序排列的结果链表。

示例说明请见LeetCode官网。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法一:链表遍历
  • 首先,如果head为null或者head只有一个结点,直接返回head;
  • 否则,从第二个结点开始遍历,记录当前结点为cur,当前的不重复的值为curVal,如果下一个结点的值等于curVal,则跳过这个结点,继续遍历下一个结点next,如果下一个结点的值不等有curVal,则更新curVal的值为下一个结点的值,且cur的下一个节点设置为next,知道遍历完成为为止,最后返回head。
public class LeetCode_083 {
    public static ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode next = head.next, cur = head;
        int curVal = head.val;
        while (next != null) {
            if (next.val == curVal) {
                next = next.next;
                cur.next = null;
            } else {
                cur.next = next;
                cur = cur.next;
                curVal = next.val;
                next = next.next;
            }
        }
        return head;
    }

    public static void main(String[] args) {
        ListNode root = new ListNode(1);
        root.next = new ListNode(1);
        root.next.next = new ListNode(2);
        root.next.next.next = new ListNode(3);
        root.next.next.next.next = new ListNode(3);
        System.out.println("=====处理前=====");
        ListNode temp = root;
        while (temp != null) {
            System.out.print(temp.val + " ");
            temp = temp.next;
        }
        System.out.println();

        deleteDuplicates(root);
        System.out.println("=====处理后=====");
        while (root != null) {
            System.out.print(root.val + " ");
            root = root.next;
        }
    }
}

【每日寄语】 但愿这漫长渺小人生,不负你每个光辉时分。

posted @   醉舞经阁  阅读(41)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示