当前位置: 首页 > news >正文

【CT】LeetCode手撕—82. 删除排序链表中的重复元素 II

题目

  • 原题连接:82. 删除排序链表中的重复元素 II

1- 思路

  • 模式识别1:已排序链表 ——> 判重逻辑 ,涉及到 while

2- 实现

⭐82. 删除排序链表中的重复元素 II——题解思路

在这里插入图片描述

class Solution {public ListNode deleteDuplicates(ListNode head) {ListNode dummyHead = new ListNode(-1);dummyHead.next = head;ListNode cur = dummyHead;while(cur.next!=null && cur.next.next!=null){if(cur.next.val==cur.next.next.val){int x = cur.next.val;while(cur.next!=null && x == cur.next.val){cur.next = cur.next.next;}}else{cur = cur.next;}}return dummyHead.next;}
}

3- ACM 实现

public class removeDuplicate {static class ListNode{int val;ListNode next;ListNode(){}ListNode(int x){val = x;}}public static ListNode removeDuplicate(ListNode head){ListNode dummyHead = new ListNode(-1);dummyHead.next = head;ListNode cur = dummyHead;while(cur.next!=null && cur.next.next!=null){if(cur.next.val==cur.next.next.val){int x = cur.next.val;while(cur.next!=null && cur.next.val == x){cur.next = cur.next.next;}}else{cur = cur.next;}}return dummyHead.next;}public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("输入链表长度");int n = sc.nextInt();ListNode head = null,tail = null;System.out.println("输入链表元素");for(int i = 0 ; i < n;i++){ListNode newNode = new ListNode(sc.nextInt());if(head==null){head = newNode;tail = newNode;}else{tail.next = newNode;tail = newNode;}}ListNode res = removeDuplicate(head);while(res!=null){System.out.print(res.val+" ");res = res.next;}}
}

http://www.lryc.cn/news/389075.html

相关文章:

  • C++ STL unique_ptr智能指针源码剖析
  • Unity中的的文件夹(特殊文件夹)
  • Go语言定时器Timer和Ticker到底怎么用
  • 41、web基础和http协议
  • 6-173 二分查找的关键字比较次数
  • 【基础篇】第5章 Elasticsearch 数据聚合与分析
  • 【网络安全】修改Host文件实现域名解析
  • Spring Boot 全面解析:从入门到实践案例
  • 222222222
  • Boost 智能指针
  • 在WSL Ubuntu中启用root用户的SSH服务
  • C语⾔数据类型和变量
  • 运行时类型信息(RTTI)
  • 使用 NVivo 定性数据分析软件指导癌症护理研究
  • R语言 | 使用ggplot绘制柱状图,在柱子中显示数值和显著性
  • 第十四届蓝桥杯省赛C++B组D题【飞机降落】题解(AC)
  • 容器化spring boot应用程序
  • 掌握智慧校园:资产来源功能解析
  • 基于公有云部署wordpress
  • vite+vue集成cesium
  • 2024 年江西省研究生数学建模竞赛A题:交通信号灯管理问题分析、实现代码及参考论文
  • 华为机试HJ1字符串最后一个单词的长度
  • 排序(冒泡排序、选择排序、插入排序、希尔排序)-->深度剖析(一)
  • (2024)docker-compose实战 (6)部署前端项目(react, vue)
  • python 中的 下划线_ 是啥意思
  • Solana公链
  • 【LeetCode】反转字符串中的单词
  • [leetcode]文件组合
  • 数据库断言
  • uniapp+nodejs实现小程序支付