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)的更多相关文章

  1. 【leetcode】Search in Rotated Sorted Array II(middle)☆

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  2. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  3. 【Leetcode】Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  4. 【链表】Remove Duplicates from Sorted List II(三指针)

    题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct  ...

  5. 【LeetCode】Search in Rotated Sorted Array II(转)

    原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...

  6. 【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 ...

  7. 【Leetcode】【Medium】Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  8. 【数组】Remove Duplicates from Sorted Array II

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  9. LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

随机推荐

  1. php加速运行优化

    一个系统的运行性能,除了程序本身要写的完善,还有要看php本身的一些问题,对于php的运行优化,主要有这些加速器:wincache,xcache,ZendOPcache,eAccelerator加速器 ...

  2. No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

    在Java中,类中的静态方法不能直接调用动态方法.只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法.所以在不做其他变动的情况下,最简单的解决办法是将public clas ...

  3. ZOJ 3711 Give Me Your Hand

    Give Me Your Hand Time Limit: 2 Seconds      Memory Limit: 131072 KB      Special Judge BellyWhite a ...

  4. Javascript包含对象的数组去重

    Array.prototype.clearRepeat = function(){ var result = [], obj = {}; for(var i = 0; i < this.leng ...

  5. 【亲述】Uber容错设计与多机房容灾方案 - 高可用架构系列

    此文是根据赵磊在[QCON高可用架构群]中的分享内容整理而成.转载请事先联系赵磊及相关编辑. 赵磊,Uber高级工程师,08年上海交通大学毕业,曾就职于微软,后加入Facebook主要负责Messen ...

  6. DateEdit和TimeEdit用法

    DateEdit 控件默认情况下,显示的只有日期,没有时间.下面介绍2中日期和时间同时显示的方法: 1.Properties.VistaDisplayMode 为true, 2.Properties. ...

  7. Two Strings Are Anagrams

    Write a method anagram(s,t) to decide if two strings are anagrams or not. 判断两个字符串里的字符是否相同,也就是是否能够通过改 ...

  8. C++变量对比java变量所占内存

    C++ char 1 short int 2 int 4 long int 8 float 4 double 8 long double 8 下面是计算程序: #include<math.h&g ...

  9. 剑指Offer 整数中1出现的次数(从1到n整数中1出现的次数)

    题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...

  10. hdu1520 树形dp Anniversary party

    A - Anniversary party Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...