【leetcode】Remove Duplicates from Sorted List II (middle)
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
.
思路:
常规思路,关键点:用伪头部避免新建链表头的麻烦。
ListNode *deleteDuplicates2(ListNode *head) {
if(head == NULL || head->next == NULL) return head;
ListNode fakehead(); //伪头结点 避免头结点建立的好方法
ListNode * newtail = &fakehead;
ListNode * p = head;
int preval = head->val + ; //记录前面结点的值 初始化为一个跟头结点值不同的数 while(p != NULL)
{
if(p->val == preval) //数字出现过
{
p = p->next;
}
else //新数字
{
preval = p->val;
if(p->next != NULL && p->next->val == preval) //新数字的下一个数字还是这个数 跳过
{
p = p->next->next;
}
else //如果确定是一个独立的数字
{
newtail->next = p;
p = p->next;
newtail = newtail->next;
newtail->next = NULL;
}
}
}
return fakehead.next;
}
大神不需要伪头部的方法,利用了指针的地址。直接更改地址里面放的指针。
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
ListNode** p = &head;
while(*p && (*p)->next) {
ListNode* p1 = *p, *p2 = p1->next;
while(p2 && p2->val == p1->val) { //直接循环到下个数字出现的位置
p2 = p2->next;
}
if(p2 != p1->next) {
*p = p2;
} else {
p = &(p1->next);
}
}
return head;
}
};
【leetcode】Remove Duplicates from Sorted List II (middle)的更多相关文章
- 【leetcode】Search in Rotated Sorted Array II(middle)☆
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 【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
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 【链表】Remove Duplicates from Sorted List II(三指针)
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- 【LeetCode】Search in Rotated Sorted Array II(转)
原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...
- 【leetcode】Remove Duplicates from Sorted Array I & II(middle)
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 【Leetcode】【Medium】Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- 【数组】Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- .Net Core 之 图形验证码
本文介绍.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能. 通过测试的系统: Windows 8.1 64bit Ubuntu Server 16.04 LTS 64 ...
- 你可能不知道的Google Chrome命令行参数
概述: 关于Google Chrome命令行参数(英文叫Google Chrome Command line switches),是Chrome为了实现实验性功能.方便调试. ...
- Mac Pro 编译安装 PHP扩展 -- Swoole扩展
回顾下先前的安装笔记: PHP5不重新编译,如何安装自带的未安装过的扩展,如soap扩展? #下载 Swoole-1.8.10后,开始编译# cd /Users/jianbao/Downloads/s ...
- C# 读写App.config
Jul142013 [C#] 读写App.config配置文件的方法 作者:xieyc 发布:2013-07-14 17:29 字符数:3433 分类:编程 阅读: 39,139 次 ...
- 关于CSS中对IE条件注释的问题
一.通用区分方式:IE6.IE7能识别*,标准浏览器(如FF)不能识别*:IE6能识别*,但不能识别 !important:IE7能识别*,也能识别 !important:IE8能识别\0,不能识别* ...
- nhibernat4.0.0.4000 bug
//类 NHibernate.Loader.Loader 中 protected virtual string[] ResultRowAliases { get { return null; } } ...
- Unity内存申请和释放
转自:http://www.jianshu.com/p/b37ee8cea04c 1.资源类型 GameObject, Transform, Mesh, Texture, Material, Shad ...
- python 临时变量使用心得
在函数里面的临时变量也可以定义为一个函数名.变量名,这样就可以通过对象来访问这个变量了,函数使用完之后不会消除.因为函数也是对象,python里面一切皆为对象.
- windows 下安装 mysql
1.根据系统,选择下载mysql community server(32/64) 版本 2.解压下载的文件 3.配置环境变量 将mysql目录下的bin目录路径配置到环境变量Path中,如下图所示 4 ...
- IC/RFID/NFC 关系与区别
IC卡 (Integrated Circuit Card,集成电路卡) 有些国家和地区也称智能卡(smart card).智慧卡(intelligent card).微电路卡(microcircuit ...