算法系列——Remove Duplicates from Sorted List II
来源:互联网 发布:恺英网络造假 编辑:程序博客网 时间:2024/06/01 10:01
题目描述
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
解题思路
引入虚拟头结点来辅助,跳过值重复的结点。
程序实现
public class Solution { public ListNode deleteDuplicates(ListNode head) { if(head==null||head.next==null) return head; ListNode dummyHead=new ListNode(-1); ListNode p=dummyHead; p.next=head; ListNode cur=head; while(cur!=null){ //跳过重复元素 while(cur.next!=null&&cur.next.val==cur.val) cur=cur.next; if(p.next==cur) p=p.next; else p.next=cur.next; cur=cur.next; } return dummyHead.next; }}
阅读全文
0 0
- 算法系列——Remove Duplicates from Sorted List II
- 算法系列——Remove Duplicates from Sorted Array II
- 算法系列—— Remove Duplicates from Sorted List
- leetcode系列(66)Remove Duplicates from Sorted List,Remove Duplicates from Sorted List II
- LeetCode——Remove Duplicates from Sorted List II
- leetcode 081 —— Remove Duplicates from Sorted List II
- LeetCode82——Remove Duplicates from Sorted List II
- Algorithms—82.Remove Duplicates from Sorted List II
- [leetcode刷题系列]Remove Duplicates from Sorted List II
- 【Leetcode长征系列】Remove Duplicates from Sorted List II
- 算法系列——Remove Duplicates from Sorted Array
- LeetCode: Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List II
- 设计模式之简单工厂
- 网页加载时间超过多少秒,你会转身离开?
- oracle sum()over函数的使用
- invalid mode 'kCFRunLoopCommonModes' provided to CFRunLoopRunSpecific
- SSMybatis整合 --详细解读Mybatis对oracle数据库进行增删改查(一)
- 算法系列——Remove Duplicates from Sorted List II
- 如何调试makefile变量
- kernel learn
- springmvc 如何跳转到根目录下的index.jsp页面
- EasyPlayerPro RTSP/RTMP/HLS/HTTP全功能流媒体播放器开发计划
- void 可以避免编译警告
- 02题
- Oracle笔记之单行函数(一)
- html属性