leetcode:Remove Duplicates from Sorted List II (留下链表中只出现一次的元素)【面试算法题】
来源:互联网 发布:php pdf完整版 编辑:程序博客网 时间:2024/04/30 22:42
题目:
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
.
依旧是链表的操作,用pre和now去删除节点,由于头节点也有可能删除,因此先建一个Head节点连头节点。
pre从Head开始,用now去判断是否重复。
如果重复,循环到下一个不同元素之前,把之间的相同元素直接跳过。
再向前移动pre和now指针。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *deleteDuplicates(ListNode *head) { ListNode *pre,*now,*Head; if(!head||!head->next)return head; Head=new ListNode(-1); Head->next=head; pre=Head; now=head; while(now&&now->next) { if(now->val == now->next->val) { while(now->next && now->val == now->next->val) { now=now->next; } pre->next=now->next; now=now->next; } else { pre=now; now=now->next; } } head=Head->next; delete(Head); return head; }};// blog.csdn.net/havenoidea
题解目录
- leetcode:Remove Duplicates from Sorted List II (留下链表中只出现一次的元素)【面试算法题】
- 【LeetCode-面试算法经典-Java实现】【082-Remove Duplicates from Sorted List II(排序链表中删除重复元素II)】
- leetcode:Remove Duplicates from Sorted List(去除链表中重复元素)【面试算法题】
- leetcode:Remove Duplicates from Sorted Array II (允许重复一次,去掉数组多余数字)【面试算法题】
- LeetCode OJ算法题(八十三):Remove Duplicates from Sorted List II
- Leetcode Remove Duplicates from Sorted List II 删除链表中重复的元素II
- leetcode系列(66)Remove Duplicates from Sorted List,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
- [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
- [leetcode] Remove Duplicates from Sorted List II
- 先锋论文检测软件,效果很好.2013年10月20日:13
- 微软正式提供Visual Studio 2013正式版下载(附直接链接汇总)
- 项目启动的时候报错:java.lang.IllegalStateException,需要配置webAppRootKey
- 排序之二(快速排序及随机化)
- 游戏禁令解除 中国将成为最大的游戏市场
- leetcode:Remove Duplicates from Sorted List II (留下链表中只出现一次的元素)【面试算法题】
- Linux下用tc控制网络延时和丢包率
- 『HTML5梦幻之旅』-滚动播放的幻灯片效果
- hdu1215
- iOS开发-IOS7自带返回手势问题
- 用HashMap模拟一个网上购物车
- Hibernate中使用分布式缓存ehcache-core-1.3.0
- HTTP错误代码
- [数字图像处理]灰度变换——直方图处理