问题:

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.

error:

开始的考虑方向错误,设置当前值和要删除的值。逻辑有点混乱。

//
//  main.cpp
//  RemoveDuplicatesfromSortedList
//
//  Created by pang stongan on 4/29/15.
//  Copyright (c) 2015 pang stongan. All rights reserved.
//#include <iostream>
using namespace std;//Definition for singly-linked list.
struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}
};class Solution {
public:ListNode* deleteDuplicates0(ListNode* head) {if(head == NULL || head->next == NULL)return head;ListNode* pre = new ListNode(-1000);pre->next = head;head = pre;ListNode* curNode = head;int curVal = pre->val;while(curNode != NULL) {
//            //单独处理尾节点
//            if(curNode->next == NULL){
//
//            }if(curVal == curNode->val){pre->next = curNode->next;} else {curVal = curNode->val;pre = curNode;}curNode = curNode->next;}return head->next;}ListNode* deleteDuplicates(ListNode* head) {if(head == NULL || head->next == NULL)return head;ListNode* pre = new ListNode(-1000);pre->next = head;head = pre;ListNode* curNode = head;int curVal = curNode->val;int count = 1;while(curNode != NULL) {if(curNode->next == NULL){if(count > 1)pre->next = NULL;elsepre->next = curNode;break;}ListNode* next = curNode->next;if(curNode->val == next->val){count++;} else if(count > 1){pre->next = next;count = 1;} else {pre->next = curNode;pre = curNode;curVal = next->val;}curNode = next;}return head->next;}
};int main(int argc, const char * argv[]) {Solution sol;ListNode* tmp = new ListNode(1);ListNode* tmp2 = new ListNode(1);ListNode* tmp3 = new ListNode(1);ListNode* tmp4 = new ListNode(2);ListNode* tmp5 = new ListNode(3);ListNode* tmp6 = new ListNode(4);ListNode* tmp7 = new ListNode(5);tmp->next = tmp2;tmp2->next = tmp3;tmp3->next = tmp4;tmp4->next = tmp5;
//        tmp5->next = tmp6;
//        tmp6->next = tmp7;ListNode* ret = sol.deleteDuplicates(tmp);while(ret){cout <<ret->val <<endl;ret = ret->next;}return 0;
}

【Leetcode】Remove Duplicates from Sorted List II相关推荐

  1. 【Leetcode】Remove Duplicates from Sorted Array II

    题目:对上一题的延伸,每个数字可以出去2次. 思路:还是设置两个下标.第一个lenxb标记已去重的地方,第二个i标记待处理的位置.每次比较时,比较lenxb和lenxb-1两个位置,如果都相等,说明出 ...

  2. 【leetcode】Remove Duplicates from Sorted Array

    题目:Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  3. LeetCode 80. Remove Duplicates from Sorted Array II

    80. Remove Duplicates from Sorted Array II My Submissions QuestionEditorial Solution Total Accepted: ...

  4. 【11_83】Remove Duplicates from Sorted List

    这道题本质上不难,难的是细节处理,容易出错. 第一遍写的代码越改越大,越臃肿,此时,不如推倒重写,果然,第二次一遍过. Remove Duplicates from Sorted List My Su ...

  5. LeetCode之Remove Duplicates from Sorted Array II

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

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

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

  7. [leetcode笔记] Remove Duplicates from Sorted List II

    问题描述: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinc ...

  8. Leetcode 82. Remove Duplicates from Sorted List II

    利用一个虚拟头节点,和维护一个前置节点. # Definition for singly-linked list. # class ListNode: # def __init__(self, x): ...

  9. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] c++

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

最新文章

  1. 【Python学习系列三】Windows下Python第三方常用库安装
  2. HDU - 3374 String Problem(最小表示法+最大表示法+KMP的next数组)
  3. 腾讯Blade Team胡珀:IoT时代,“白帽子”以网为剑捍卫安全
  4. Java案例:通过循环打印一颗爱心
  5. python勿使用mutable值作为默认参数
  6. 有关计算机程序的论文,有关计算机程序的论文范文.doc
  7. android 中xml文件的各种样式的学习
  8. NMS(非极大值抑制)算法 -- 理论、代码
  9. java简历编写及面试技巧
  10. 飞天2.0面向万物智能的操作系统正式启幕
  11. c语言打印七芒星图形代码,强强纯爱文,看过《C语言修仙》《七芒星》也别错过《狼行成双》...
  12. 基于java后台微信图书商城小程序系统 开题报告
  13. java(基础语法)
  14. 抖音自主品牌号该发什么作品
  15. ​五分钟扫盲:进程与线程基础必知
  16. Unicode和GB2312编码表并转换为ASCII字符
  17. 递归方法求最大公约数,求最小公倍数
  18. Linux C语言IO多路转接select函数
  19. 沙特能效2902标签、证书办理,SASO2902流程
  20. 360软件管家下载|360软件管家电脑版下载

热门文章

  1. git基本命令ls、cat、mv、touch简述
  2. 书生"商友营销引擎"升级计划
  3. 外汇交易策略如何制定
  4. SwipeRefreshLayout+CardView+RecyclerView 精美瀑布流效果
  5. 最全中级Unity面试题(引擎,渲染,Lua等)
  6. [4750g] 升级BIOS 禁用独显解决linux下发热问题
  7. WSL 双系统端口映射,网络穿透最新教程
  8. 旅行的意义:写给不爱旅行的你
  9. php5.3.13 apc,PHP安装APC扩展
  10. 数字化工厂该从何做起?