Remove Duplicates from Sorted ListII
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
示例 1:
输入: 1->2->3->3->4->4->5
输出: 1->2->5
示例 2:
输入: 1->1->1->2->3
输出: 2->3 思路:(1)因为头结点可能被删除,所以要定义头结点;(2)定义两个指针,一个指向前驱结点,另一个指针指向前驱节的下一个结点,并不断找到值相等的结点。
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) { if(!head || !head->next)
return head; ListNode* dummy = new ListNode(-); ListNode* p = dummy;
dummy->next = head;
while(p->next)
{
ListNode* cur = p->next; while(cur->next && cur->val == cur->next->val)
cur = cur->next; if(cur != p->next)
p->next = cur->next;
else
p = p->next; } return dummy->next;
}
};
Remove Duplicates from Sorted ListII的更多相关文章
- [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Leetcode-83 Remove Duplicates from Sorted List
#83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...
- Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 26. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
随机推荐
- python(十一)面向切面编程AOP和装饰器
二.装饰器 装饰器可以在给函数修改功能的同时并不改变这个函数本身.(以下用的都是python2.7) 首先,在python里面函数是对象,在下面的函数里"fun"是函数也是对象可以 ...
- C - Alphabetic Removals
题目链接: You are given a string ss consisting of nn lowercase Latin letters. Polycarp wants to remove e ...
- DeepLearning.ai-Week3-Autonomous driving-Car detection
1 - Import Packages import argparse import os import matplotlib.pyplot as plt from matplotlib.pyplot ...
- python - 字符编码/格式化/替换符
- Css - 利于搜索引擎收录的三个标签
Css - 利于搜索引擎收录的三个标签 <head> <meta charset="utf-8" /> <title>京东(JD.COM)-正品 ...
- scrapy基础 之 xpath网页结构
1 ,什么是xpath XPath 是一门在 XML 文档中查找信息的语言.XML是一种类似于HTML的传输协议 2,节点 XPath 中,有七种类型的节点:元素.属性.文本.命名空间.处理指令.注释 ...
- 将mnist数据集保存成numpy格式
import numpy as np from urllib import request import gzip import pickle filename = [ ["training ...
- 安卓虚拟机与Hyper-V冲突
经过各种经验,哪个安卓虚拟机跟Hyper-V都存在着冲突. 解决方案一 程序中卸载Hyper-V,之后还要再配置太麻烦. 解决方案二 1.关掉Hyper-V的启动项,命令如下. bcdedit /se ...
- macbook install mysql
安装Homebrew,详细步骤参见Homebrew官网. brew doctor确认brew在正常工作. brew update更新包. brew install mysql 安装mysql.log如 ...
- ppp 完全理解(二)【转】
转自:https://blog.csdn.net/tianruxishui/article/details/44057717 ppp 完全理解(二) pppd 协议及代码分析 作者:李圳均 日期:20 ...