首页 > 其他分享 >(链表)16-删除有序链表中重复的元素-b

(链表)16-删除有序链表中重复的元素-b

时间:2023-11-17 22:57:17浏览次数:26  
标签:ListNode cur 删除 16 next 链表 val null

 1 import java.util.*;
 2 
 3 /*
 4  * public class ListNode {
 5  *   int val;
 6  *   ListNode next = null;
 7  *   public ListNode(int val) {
 8  *     this.val = val;
 9  *   }
10  * }
11  */
12 public class Solution {
13     /**
14      * @param head ListNode类
15      * @return ListNode类
16      */
17     public ListNode deleteDuplicates (ListNode head) {
18         // 判空链表
19         if (head == null) {
20             return null;
21         }
22         // 增加临时表头
23         ListNode node = new ListNode(0);
24         node.next = head;
25         // 申请遍历指针
26         ListNode cur = node;
27         // 遍历链表
28         while (cur.next != null && cur.next.next != null) {
29             if (cur.next.val == cur.next.next.val) {
30                 // 相邻两个节点值相同
31                 int deleteValue = cur.next.val;
32                 // 将所有相同的都跳过
33                 while (cur.next != null && cur.next.val == deleteValue) {
34                     cur.next = cur.next.next;
35                 }
36             } else {
37                 // 相邻两个节点值不同
38                 cur = cur.next;
39             }
40         }
41         // 返回时删除表头
42         return node.next;
43     }
44 }

 

标签:ListNode,cur,删除,16,next,链表,val,null
From: https://www.cnblogs.com/StringBuilder/p/17839837.html

相关文章

  • (链表)10-相交链表
    1/*2publicclassListNode{3intval;4ListNodenext=null;56ListNode(intval){7this.val=val;8}9}*/10publicclassSolution{11publicListNodeFindFirstCommonNode(ListNodepHead1,ListNodepHead2)......
  • (链表)15-删除有序链表中的重复元素-a
    1importjava.util.*;23/*4*publicclassListNode{5*intval;6*ListNodenext=null;7*publicListNode(intval){8*this.val=val;9*}10*}11*/12publicclassSolution{13/**14*@paramhead......
  • 11.16每日总结
     昨天博客忘记发了,补一下。 昨天上课王老师强调了智能排产。昨天在做一个可以实现不同表都可以调用的增函数。publicstaticvoidinsertData(StringtableName,String...columnValuePairs){Connectionconnection=getConnection();try{......
  • PTAC语言删除字符串中的字串
    这是题目。初见觉得还好,谁知道越分析越操蛋暗含深意。仔细看,假设我们通过遍历s1删除了两个显性的cat,哎,剩下的是什么Tomisamalecat咋样,牛逼不。说明这题肯定会出现删除一次不够的样例sample。假设我们熟知C语言中#include<string.h>中的strcat,strstr,strcpy等函数,那么这题可以比......
  • mysql中删除数据中的特殊字符
    --此字符只影响终端的查询,如crt,xshell等,在navicat中不受影响selectstr,replace(str,char(13),'')asstr_fixfrom(selectconcat('1',char(13),'2')asstr)t1;......
  • VIM 使用技巧 —— 全选删除
    操作步骤确保进入普通模式,按下ESC跳转到文件开头——gg进入可视模式——V,也就是shift+v将光标移到文件末尾——G,也就是shift+g删除——d解释1.可视模式可视模式允许我们选择一块文本区域,然后在上面进行编辑,比如删除、替换等。Vim中有在三种可视模式:......
  • 学期2023-2024-1 20231416《计算机基础与程序设计》第八周学习总结
    作业信息这个作业属于哪个课程2023-2024-1-计算机基础与程序设计这个作业要求在哪里2023-2024-1计算机基础与程序设计第八周作业这个作业的目标《计算机科学概论》第9章《C语言程序设计》第7章并完成云班课测试,功能设计与面向对象设计,面向对象设计过程,面向对象语言三......
  • QQ邮件群发工具批量版,支持163邮箱,可搭建SMTP邮局,易语言全开源
    不借助任何易语言的模块,纯代码编写的,运行非常流畅,我之前自己发QQ邮件用的,每天几千份没问题,近期因为一些工作上的原因就想着把软件开源出来,一些好的思路就免费分享出来,软件核心发送模块整合到了“jmail.dll这个文件大家网上估计能搜到,我下面分享的代码是所有代码,并不是单个窗口的代......
  • 双向循环链表应用
    已知p指向双向循环链表中的一个结点,其结点结构为data、prior、next三个域,实现交换p所指向的结点和它的前缀结点的顺序。#include<iostream>#include<cstring>usingnamespacestd;typedefstructf{intdata;f*next;f*pre;}node,*Node;voidbuild(Nodep){int......
  • 链表逆置
     structListNode*reverse(structListNode*head){if(head==NULL||head->next==NULL){returnhead;}structListNode*p=NULL,*t,*pp=head;while(pp){t=pp->next;pp->next=p;p=pp;pp=t;}......