当前位置:网站首页>JZ76 删除链表中重复的结点
JZ76 删除链表中重复的结点
2022-07-20 21:38:00 【syc596】
JZ76 删除链表中重复的结点
删除链表中重复的结点_牛客题霸_牛客网 (nowcoder.com)
import java.util.*;
public class Solution {
public ListNode deleteDuplication(ListNode head) {
if(head==null){
return null;
}
ListNode newHead=new ListNode(-1);
newHead.next=head;
ListNode cur=newHead;
while(cur.next!=null&&cur.next.next!=null){
if(cur.next.val==cur.next.next.val){
int tmp=cur.next.val;
while(cur.next!=null&&cur.next.val==tmp){
cur.next=cur.next.next;
}
}else{
cur=cur.next;
}
}
return newHead.next;
}
}
//map
import java.util.*;
import java.util.HashMap;
public class Solution {
public ListNode deleteDuplication(ListNode head) {
if(head==null){
return null;
}
HashMap<Integer,Integer> map=new HashMap<>();
ListNode cur=head;
while(cur!=null){
if(map.containsKey(cur.val)){
map.put(cur.val,map.get(cur.val)+1);
}else{
map.put(cur.val,1);
}
cur=cur.next;
}
ListNode newHead=new ListNode(-1);
newHead.next=head;
cur=newHead;
while(cur.next!=null){
if(map.get(cur.next.val)!=1){
cur.next=cur.next.next;
}else{
cur=cur.next;
}
}
return newHead.next;
}
}
边栏推荐
- One Oracle statement to update or add
- Dama Chapter 8 (data integration and interoperability)
- This domestic API artifact tool is too strong Let me give up postman
- I2C client driver
- Wang fried power to create wealth fast! Xiangling giant panda 2.0 power products officially launched
- Difference between service and systemctl / method of modifying path / a command to view IP address, gateway and DNS server
- 2022 eye care lamp exhibition, traditional Chinese medicine eye conditioning exhibition, Shandong eye health exhibition, China eye Expo
- . Net core load assembly assemblyloadcontext
- Training of head and neck segmentation networks with shape prior on small datasets
- i2c adapter驱动
猜你喜欢
Solve the problem of error reporting in NPM installation NRM syntaxerror: unexpected token import
Blueprism tool menu bar user rights function description -rpa Chapter 2
專注跨境支付一體化服務 iPayLinks榮獲《財資》(The Asset)3A亞洲獎!
Lora base station coverage
Wechat vaccine appointment applet graduation project of applet completion work (4) opening report
Database constraint & MySQL advanced query
C | | structure consortium enumeration type
DTOS帝拓思的3D引擎將取代遊戲引擎巨獸,實現國產化替代
[mqtt from introduction to improvement series | 08] mqtt3.1.1 topic details
Wechat vaccine appointment applet graduation project of applet completion work (7) mid term inspection report
随机推荐
Opengauss kernel analysis: query rewriting
If no linetype is selected, the reason why cacuilinetypecombobox crashes
How to conduct user retention analysis?
Several stumbling blocks of it entrepreneurs
Modify the computer registry and add vscode to the right-click menu
Draw circles and rectangles with VTK controls
Five reasons why developers use Klocwork for software security
One Oracle statement to update or add
Xqlla2.3.2 parsing query
Easygbs platform setup tips: how to hide the platform web page from being accessed?
Multi thread anti conflict
Set current style dimstyle
Signal integrity (SI) power integrity (PI) learning notes (XXIX) power distribution network (I)
Wechat vaccine reservation applet graduation design of applet completion work (3) background function
2022第五届中国(济南)国际青少年眼健康产业展览会9月举办
2.8 component life cycle
DTOS帝拓思的3D引擎将取代游戏引擎巨兽,实现国产化替代
Upload a folder (including subfolders and files) in VSS
每日一题-LeetCode1260-二维网格迁移-数组-映射
What are the methods to realize timeout in go language