LeetCode CodeTop 82.删除排序链表中的重复元素Ⅱ

1.思路:两个指针分别指向cur.next和cur.next.next,如果发现重复就一次性删除所有值为val的节点;否则cur正常前进一位。

2.复杂度分析:

(1)时间复杂度:O(n),其中n为链表的长度。

(2)空间复杂度:O(1),仅用到若干额外变量。

附代码:

java 复制代码
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode sentinel = new ListNode();
        sentinel.next = head;
        ListNode cur = sentinel;
        while(cur.next != null && cur.next.next != null){
            int val = cur.next.val;
            // 后两个节点值相同,都为val
            if(val == cur.next.next.val){
                // 从第一个值等于val的节点开始,将值等于val的节点全部删除
                while(cur.next != null && cur.next.val == val){
                    cur.next = cur.next.next;
                }
            }else{
                cur = cur.next;
            }
        }
        return sentinel.next;
    }
}

ACM模式:

java 复制代码
import java.util.Scanner;

// 将 ListNode 定义为独立的类
class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) {
        this.val = val;
        this.next = null;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 读取链表长度
        int n = scanner.nextInt();

        // 读取链表元素
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = scanner.nextInt();
        }

        // 创建链表
        ListNode head = null;
        if (n > 0) {
            head = new ListNode(nums[0]);
            ListNode current = head;
            for (int i = 1; i < n; i++) {
                current.next = new ListNode(nums[i]);
                current = current.next;
            }
        }

        // 调用删除重复元素方法
        Solution solution = new Solution();
        ListNode result = solution.deleteDuplicates(head);

        // 输出删除重复后的链表
        if (result == null) {
            System.out.println("");
        } else {
            ListNode current = result;
            while (current != null) {
                System.out.print(current.val);
                if (current.next != null) {
                    System.out.print(" ");
                }
                current = current.next;
            }
            System.out.println();
        }

        scanner.close();
    }
}

// 删除重复元素方法放在 Solution 类中
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode sentinel = new ListNode();
        sentinel.next = head;
        ListNode cur = sentinel;
        while (cur.next != null && cur.next.next != null) {
            int val = cur.next.val;
            // 后两个节点值相同,都为val
            if (val == cur.next.next.val) {
                // 从第一个值等于val的节点开始,将值等于val的节点全部删除
                while (cur.next != null && cur.next.val == val) {
                    cur.next = cur.next.next;
                }
            } else {
                cur = cur.next;
            }
        }
        return sentinel.next;
    }
}
相关推荐
过期动态3 分钟前
【LeetCode 热题 100】找到字符串中所有字母异位词
java·数据结构·算法·leetcode·职场和发展·rabbitmq
来一碗刘肉面12 分钟前
栈在递归中的应用
数据结构·算法
8K超高清23 分钟前
博冠获中国电影电视技术学会科技进步奖
人工智能·科技·算法·安全·接口隔离原则·智能硬件
微石科技44 分钟前
长期慢病如何做好居家管理?宁波微石科技星梦云康改善周期数据零散短板
大数据·科技·物联网·算法
微露清风1 小时前
快速排序算法学习记录
学习·算法·排序算法
梓䈑6 小时前
【算法题攻略】BFS 解决FloodFill 算法、最短路问题、多源BFS 和 拓扑排序
c++·算法·宽度优先
Adios7949 小时前
设置交集大小至少为2
数据结构·算法·leetcode
来一碗刘肉面15 小时前
栈的应用(表达式求值)
数据结构·算法
xiaowang1234shs16 小时前
怪兽轻断食技术深度测评:从断食计时引擎到AI识别算法的工程实践解析
数据库·人工智能·算法·macos·机器学习·p2p·visual studio