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 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 == NULL || head->next == NULL) {
- return head;
- }
- ListNode * dummy = new ListNode(INT_MIN);
- dummy->next = head;
- head = dummy;
- int duplicate;
- while (head->next != NULL && head->next->next != NULL) {
- if (head->next->val == head->next->next->val) {
- duplicate = head->next->val;
- while (head->next != NULL && head->next->val == duplicate) {
- ListNode * temp = head->next;
- free(temp);
- head->next = head->next->next;
- }
- }
- else {
- head = head->next;
- }
- }
- return dummy->next;
- }
- };
nc
解法二:
- class Solution {
- public:
- ListNode* deleteDuplicates(ListNode* head) {
- if(!head) return head;
- ListNode dummy();
- dummy.next = head;
- ListNode *pre = &dummy;
- ListNode *cur = head;
- while(cur && cur->next){
- while(cur->next && cur->val == cur->next->val) cur = cur->next;
- if(pre->next == cur){
- pre = cur;
- cur = cur->next;
- } else {
- cur = cur->next;
- pre->next = cur;
- }
- }
- return dummy.next;
- }
- };
参考了@liismn 的代码
解法三:
- class Solution {
- public:
- ListNode* deleteDuplicates(ListNode* head) {
- if(!head||!head->next) return head;
- ListNode* dummy = new ListNode();
- ListNode* tail = dummy;
- int flag = true; // should the current head be added ?
- while(head){
- while(head&&head->next&&head->val==head->next->val)
- {
- flag = false; // finds duplicate, set it to false
- head = head->next;
- }
- if(flag) // if should be added
- {
- tail->next = head;
- tail = tail->next;
- }
- head = head->next;
- flag = true; // time for a new head value, set flag back to true
- }
- tail->next = nullptr; // Don't forget this... I did..
- return dummy->next;
- }
- };
参考了@GoGoDong 的代码
解法四:
- public ListNode deleteDuplicates(ListNode head) {
- if (head == null) return null;
- if (head.next != null && head.val == head.next.val) {
- while (head.next != null && head.val == head.next.val) {
- head = head.next;
- }
- return deleteDuplicates(head.next);
- } else {
- head.next = deleteDuplicates(head.next);
- }
- return head;
- }
递归,参考了@totalheap 的代码,还不太明白
82. Remove Duplicates from Sorted List II【Medium】的更多相关文章
- leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)
203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...
- 82. Remove Duplicates from Sorted List II && i
题目 83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such tha ...
- 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- LeetCode OJ 82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode#82]Remove Duplicates from Sorted Array II
Problem: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? F ...
- 【一天一道LeetCode】#82. Remove Duplicates from Sorted List II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【Leetcode】82. Remove Duplicates from Sorted List II
Question: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only dis ...
随机推荐
- 【单调队列优化DP】BZOJ1855-[Scoi2010]股票交易
[题目大意] 已知第i天的股票买入价为每股APi,第i天的股票卖出价为每股BPi(数据保证对于每个i,都有APi>=BPi),第i天的一次买入至多只能购买ASi股,一次卖出至多只能卖出BSi股. ...
- python3 中 and 和 or 运算规律
一.包含一个逻辑运算符 首先从基本的概念着手,python中哪些对象会被当成 False 呢?而哪些又是 True 呢? 在Python中,None.任何数值类型中的0.空字符串“”.空元组().空列 ...
- 计算数字出现的次数 Exercise07_03
import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:计算数字出现的次数 * */ public class Exercise0 ...
- 五角数 Exercise06_01
/** * @author 冰樱梦 * 题目:五角数 * 时间:2018年下半年 * * */ public class Exercise06_01 { public static void main ...
- 专访阿里巴巴研究员“赵海平”:Facebook的PHP底层性能优化之路(HipHop,HHVM)
专访阿里巴巴研究员“赵海平”:Facebook的PHP底层性能优化之路 http://www.infoq.com/cn/articles/interview-alibaba-zhaohaiping
- 调试SQLSERVER (二)使用Windbg调试SQLSERVER的环境设置 ------符号文件
http://www.cnblogs.com/lyhabc/p/4184708.html
- 【java】递归统计本地磁盘所有文件,提取重复文件,JDK8 map迭代
package com.sxd.createDao; import java.io.File; import java.time.LocalDateTime; import java.util.Has ...
- 让IE浏览器支持HTML5
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- coco2dx-2.2.2 win32启动过程(opengl 和 窗口大小初始化部分) - 学习笔记 1
因为最近要做不同分辩率的适配,所于看了下引擎这方面的代码,记录一下当是学习笔记,cocos2d-x 版本 2.2.2 , 例子是samples\Cpp\TestCpp下的 TestCpp. 先看下ma ...
- apache poi合并单元格设置边框
HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); //创建一个样式 HSSFCellStyle sty ...