【Remove Duplicates from Sorted List II 】cpp
题目:
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.
代码:
/**
* 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) {
if ( !head || !head->next ) return head;
ListNode dummy(INT_MIN);
dummy.next = head;
ListNode *prev = &dummy;
ListNode *p = head;
while ( p && p->next )
{
if ( p->val!=p->next->val )
{
prev = p;
p = p->next;
}
else
{
while ( p->next && p->val==p->next->val ) p = p->next;
prev->next = p->next;
p = p->next;
}
}
return dummy.next;
}
};
Tips:
主要思路就是:如果遇上相同的,就用while循环一直往后过。
具体思路沿用了之前Python版的:http://www.cnblogs.com/xbf9xbf/p/4186852.html
====================================================
第二次过这道题,思路忘记了。赶紧翻了了一下上面的笔记,才想起来;代码一次AC了。
/**
* 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) {
if ( !head ) return head;
ListNode dummpy(-);
dummpy.next = head; ListNode* pre = &dummpy;
ListNode* curr = head; while ( curr && curr->next )
{
if ( curr->val!=curr->next->val )
{
pre = curr;
curr = curr->next;
}
else
{
while ( curr->next && curr->val==curr->next->val )
{
curr = curr->next;
}
pre->next = curr->next;
curr = curr->next;
}
}
return dummpy.next;
}
};
这里的思路关键点是while循环的判断条件(curr && curr->next)。
【Remove Duplicates from Sorted List II 】cpp的更多相关文章
- 【Remove Duplicates from Sorted Array II】cpp
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- leetcode 【 Remove Duplicates from Sorted Array II 】python 实现
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- leetcode 【 Remove Duplicates from Sorted List II 】 python 实现
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- 【Search in Rotated Sorted Array II 】cpp
题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...
- 82. Remove Duplicates from Sorted List II【Medium】
82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【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】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
随机推荐
- 如何查看win10已激活密钥?查看win10已激活完整密钥的方法!
如何查看win10已激活密钥?查看win10已激活完整密钥的方法! HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVersion/So ...
- 跨平台移动开发phonegap/cordova 3.3全系列教程-开发环境搭建
操作系统:windwos xp 1. 安装JDK 打开如下网站下载JDK http://www.oracle.com/technetwork/java/javase/downloads/index ...
- jq动态增加的button标签click回调失效的问题,即动态增加的button标签绑定事件$("button.class").click(function)无效
对于新增加的页面元素,改变了页面结构,如果是使用老办法$("button.class").click(function)去监听新的button标签事件,会失效. 笔者的应用是文字的 ...
- mybatis-动态sql2
mybatis的动态sql中常用的有 if where foreach set 项目沿用之前的. 1.dao层添加接口: package com.java1234.map ...
- HDU3973 线段树 + 字符哈希
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3973 , 线段树 + 字符哈希,好题. 又学了一种新的哈希方法,hhhh~ 解法: 想法是用P进制的数 ...
- UOJ#210. 【UER #6】寻找罪犯 2-sat
#210. [UER #6]寻找罪犯 链接:http://uoj.ac/problem/210 想法:2-sat模型.每个人拆点,分别表示为犯人.非犯人.每个句供词拆点,分别表示真话.假话.供词与对应 ...
- log4j.properties中的这句话“log4j.logger.org.hibernate.SQL=DEBUG ”该怎么写在log4j.xml里面呢?
http://www.cnblogs.com/gredswsh/p/log4j_xml_properties.html 请问:log4j.properties中的这句话“log4j.logger.or ...
- Using an Image for the Layer’s Content
Using an Image for the Layer’s Content Because a layer is just a container for managing a bitmap ima ...
- Linux系统运维常见面试简答题(36题)
1.请描述下linux 系统的开机启动过程开机加电BIOS自检———–>MBR引导———–>grub引导菜单———–>加载内核———–>启动init进程———–>读取in ...
- Spring学习记录(一)
1.Spring简介 Spring是一个轻量级的java开发框架.框架的主要优势之一就是分层架构,Spring使用基本的JavaBean,不仅限于服务器的开发.从简单性,可测试性和松耦合的角度而言,任 ...