【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
.
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相关推荐
- 【Leetcode】Remove Duplicates from Sorted Array II
题目:对上一题的延伸,每个数字可以出去2次. 思路:还是设置两个下标.第一个lenxb标记已去重的地方,第二个i标记待处理的位置.每次比较时,比较lenxb和lenxb-1两个位置,如果都相等,说明出 ...
- 【leetcode】Remove Duplicates from Sorted Array
题目:Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode 80. Remove Duplicates from Sorted Array II
80. Remove Duplicates from Sorted Array II My Submissions QuestionEditorial Solution Total Accepted: ...
- 【11_83】Remove Duplicates from Sorted List
这道题本质上不难,难的是细节处理,容易出错. 第一遍写的代码越改越大,越臃肿,此时,不如推倒重写,果然,第二次一遍过. Remove Duplicates from Sorted List My Su ...
- LeetCode之Remove Duplicates from Sorted Array II
1.题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...
- 【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 List II
问题描述: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinc ...
- Leetcode 82. Remove Duplicates from Sorted List II
利用一个虚拟头节点,和维护一个前置节点. # Definition for singly-linked list. # class ListNode: # def __init__(self, x): ...
- 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++> 给出排序好的 ...
最新文章
- 【Python学习系列三】Windows下Python第三方常用库安装
- HDU - 3374 String Problem(最小表示法+最大表示法+KMP的next数组)
- 腾讯Blade Team胡珀:IoT时代,“白帽子”以网为剑捍卫安全
- Java案例:通过循环打印一颗爱心
- python勿使用mutable值作为默认参数
- 有关计算机程序的论文,有关计算机程序的论文范文.doc
- android 中xml文件的各种样式的学习
- NMS(非极大值抑制)算法 -- 理论、代码
- java简历编写及面试技巧
- 飞天2.0面向万物智能的操作系统正式启幕
- c语言打印七芒星图形代码,强强纯爱文,看过《C语言修仙》《七芒星》也别错过《狼行成双》...
- 基于java后台微信图书商城小程序系统 开题报告
- java(基础语法)
- 抖音自主品牌号该发什么作品
- ​五分钟扫盲:进程与线程基础必知
- Unicode和GB2312编码表并转换为ASCII字符
- 递归方法求最大公约数,求最小公倍数
- Linux C语言IO多路转接select函数
- 沙特能效2902标签、证书办理,SASO2902流程
- 360软件管家下载|360软件管家电脑版下载