当前位置:网站首页>leetcode 82. Delete duplicate Element II in the sorting linked list
leetcode 82. Delete duplicate Element II in the sorting linked list
2022-06-27 16:59:00 【chenyson】
difficulty : secondary
The frequency of :77
subject : Given the header of a sorted linked list head , Delete all nodes with duplicate numbers in the original linked list , Leave only different numbers . return Sorted linked list .
Their thinking : One traverse
Be careful :
- Traversal time , If the values of two nodes are equal , that cur The pointer moves
- most important of all , Not equal to the time of processing , But when they are not equal
- When it's not equal , To judge ,pre and cur It's not a relationship
- If it's still contextual , Then put the pre Move to cur
- If it's not context , That explains. pre And then to cur( Include cur) They are all equal , So directly put pre Connect to cur The next node of
- When it's not equal , To judge ,pre and cur It's not a relationship
- Finally, because it is traversal , So definitely cur To move
- To sum up : Move when equal cur, When it's not equal pre and cur All move , At the end of a cycle, one after another
Code :
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode dummyhead=new ListNode(-1);
dummyhead.next=head;
ListNode cur=head,pre=dummyhead;
while(cur!=null){
// Because it's traversal , So this condition is certain
while(cur.next!=null&&cur.val==cur.next.val){
Because of the use of cur.next.val, therefore cur.next!=null
// also cur.next!=null It has to be in the front , Otherwise, a null pointer exception will occur
cur=cur.next;
}
//pre and cur Connected to each other , You don't have to do much
if(pre.next==cur){
pre=cur;
}
//pre and cur There are equal in the middle also cur Equal to the middle
else{
pre.next=cur.next;
}
cur=cur.next;
}
return dummyhead.next;
}
}
边栏推荐
- C système de gestion de la charge de travail des enseignants en langues
- Oracle概念三
- 华为云DevCloud重磅发布四大新能力,创下国内两项第一
- 华为云首次解读云原生2.0十大典型架构,加速构建现代化应用
- The time of localdatetime type (2019-11-19t15:16:17) is queried with the time range of Oracle
- LeetCode 124. Binary tree maximum path sum - binary tree series question 8
- 模拟进程调度
- QT5.5.1桌面版安装配置过程中的疑难杂症处理(配置ARM编译套件)
- C language teacher workload management system
- Oracle概念二
猜你喜欢

数据中心表格报表实现定制统计加班请假汇总记录分享

树莓派初步使用

How to improve it electronic equipment performance management

Event listening mechanism

Bit. Store: long bear market, stable stacking products may become the main theme

Data center table reports realize customized statistics, overtime leave summary record sharing

Detailed explanation of various GPIO input and output modes (push-pull, open drain, quasi bidirectional port)

Leetcode 5. Longest Palindromic Substring

【多线程】线程通信调度、等待集 wait() 、notify()

EMQ 助力青岛研博建设智慧水务平台
随机推荐
Simulated process scheduling
National food safety risk assessment center: do not blindly and unilaterally pursue "zero addition" and "pure natural" food
深耕数字化,引领云原生,服务更多开发者
#yyds干货盘点#简述chromeV8引擎垃圾回收
智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维
软件测试-测试的概念,单元测试的详细介绍,如何设计测试用例
字节跳动埋点数据流建设与治理实践
国家食品安全风险评估中心:不要盲目片面追捧标签为“零添加”“纯天然”食品
Raspberry pie preliminary use
C語言教師工作量管理系統
【多线程】线程通信调度、等待集 wait() 、notify()
郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮
tensorflow求解泊松方程
Etcd可视化工具:Kstone部署(一),基于Helm快速部署
等保2.0密码要求是什么?法律依据有哪些?
模拟进程调度
The array of C language is a parameter to pass a pointer
d3dx9_ What if 27.dll is lost? d3dx9_ How to repair 27.dll?
Array represents a collection of several intervals. Please merge all overlapping intervals and return a non overlapping interval array. The array must exactly cover all the intervals in the input. 【Le
How to improve it electronic equipment performance management