[LeetCode] Remove Duplicates from Sorted List II
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.
Before I get started, I should write down what I know for now, its a singly-linked-list related problem, and its already been sorted, that mean every duplicate numbers is next to each other. note that it requires to remove all the duplicates, seems like an easy job.
ListNode *deleteDuplicates(ListNode *head) {
ListNode* current, *prev;
ListNode base = ListNode(-10);
base.next = head;
current = &base; prev = NULL;
while (current && current->next){
if (current->val == current->next->val){
while (current->next && current->val == current->next->val){
current = current->next;
}
prev->next = current->next;
current = current->next; }else {
prev = current;
current = current->next;
} }
return base.next;
}
By inserting a dummy node with some invalid number(I choose -1 at first, but a test case uses -1 as 1st val as well...wtf) before head is a good trick, it'll allow you to delete head of current list. anyway, operating linked list in c++ is much convenient then c. In c, you can't change list structure by not involving any double pointer.
[LeetCode] Remove Duplicates from Sorted List II的更多相关文章
- Leetcode: Remove Duplicates from Sorted List II 解题报告
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [Leetcode] Remove Duplicates From Sorted Array II (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array II [27]
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- [leetcode]Remove Duplicates from Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...
- [leetcode]Remove Duplicates from Sorted List II @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题意: Given a sorted link ...
- LeetCode::Remove Duplicates from Sorted List II [具体分析]
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
随机推荐
- WebClient 访问https
解决SSH证书问题: webClient.getOptions().setUseInsecureSSL(true);//解决ssh证书访问https的问题
- Jsoup Element网页信息采集
package zeze; import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; i ...
- C#调用有有参有返的存储过程
(1)在SQL Server中建立如下的存储过程: set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOCREATE PROCEDURE [dbo].[GetName ...
- Adapter
11-25 16:09:10.965 22791-22823/myapplication.com.myblue E/HAL: hw_get_module_by_class: lib loaded: / ...
- Intellij IDEA + Jrebel
Jrebel java热部署神器! 如果你没用过这里一句白话说明,就是在修改了class等源文件后无需重启web容器(如Tomcat)直接生效! 找到以后 安装... 然后 激活地址 :http:// ...
- js手机网页跳转
在网页头部加入如下代码: <script type="text/javascript"> function browserRedirect() { var sUserA ...
- poj 3126
一道搜索的水题,其实搜索相对其他的来说好掌握一点,因为有个固定的模板可以用去套 题目大意就是数字的变化,一个数字只可以变化到它最相邻的一个素数上去,意思是只变化一位数字,求最短的变化方案 #inclu ...
- springMVC 访问404
问题:404 但是其他的controller可以访问!!!
- Appium+Robotframework实现Android应用的自动化测试-1:Appium在Windows中的安装
让我们开始在Windows中开始安装Appium吧,Appium在OS X中的具体安装后面的文章会介绍. 另外,官网上说先要装Node.js,还要装Apache Ant和Apache Maven,Gi ...
- WinAPI: ShellExecute - 打开外部程序或文件
WinAPI: ShellExecute - 打开外部程序或文件 ShellExecute( hWnd: HWND; {指定父窗口句柄} Operation: PChar; { ...