48. Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example, Given 1->1->2
, return 1->2
. Given 1->1->2->3->3
, return 1->2->3
.
思路: Easy. 依第二个开始,从前往后走一遍,若下一个相同,则删掉,迈过去,否则,走一步。
/**
* 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) {
ListNode *p = head;
while(p && p->next) {
if(p->val == p->next->val) {
ListNode *q = p->next;
p->next = p->next->next;
free(q);
}
else p = p->next;
}
return head;
}
};
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
.
思路,由于前面几个相同时,比较麻烦,难以统一处理。故在头结点前面再加一个伪头结点(值小于头结点),这样前后走一遍即可。(代码未释放空间)
/**
* 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) {
ListNode *pseudoHead = new ListNode(0);
pseudoHead->next = head;
ListNode *pre, *cur;
pre = pseudoHead;
while(head) {
for(cur = head->next; cur && cur->val == head->val; cur = cur->next);
if(head->next != cur) {
pre->next = cur;
head = cur;
}
else { pre = head; head = head->next; }
}
return pseudoHead->next;
}
};
或
/**
* 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) {
ListNode pseudoHead(0);
pseudoHead.next = head;
ListNode *pre, *cur;
pre = &pseudoHead;
while(head) {
for(cur = head->next; cur && cur->val == head->val; cur = cur->next);
if(head->next != cur) {
pre->next = cur;
head = cur;
}
else { pre = head; head = head->next; }
}
return pseudoHead.next;
}
};
48. Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II的更多相关文章
- 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...
- LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II
1. Remove Duplicates from Sorted List 题目链接 题目要求: Given a sorted linked list, delete all duplicates s ...
- Merge Two Sorted Lists & Remove Nth Node From End of List
1.合并两个排好序的list Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The ...
- [Algorithm] Count occurrences of a number in a sorted array with duplicates using Binary Search
Let's say we are going to find out number of occurrences of a number in a sorted array using binary ...
- apt-get remove, apt-get autoremove和aptitude remove的区别
这篇文章的图片链接发生了问题,无法正常查看图片,所以我在CSDN转载一下,特此声明. apt-getremove的行为我们很好理解,就是删除某个包的同时,删除依赖于它的包,例如:A依赖于B, B依赖于 ...
- pycharm的python console报错CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_ipython_console_011.py", line 87, in init self.matchers.remove(self.python_matches) ValueError: list.remove(x): x not in list
卸载ipython pip uninstall ipython 安装ipython6.2.0 pip install ipython==6.2.0
- 49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- Search in Sorted Array,Search in Rotated Sorted Array,Search in Rotated Sorted ArrayII
一:Search in Sorted Array 二分查找,可有重复元素,返回target所在的位置,只需返回其中一个位置,代码中的查找范围为[low,high),左闭右开,否则容易照成死循环. 代码 ...
随机推荐
- 二模 (15)day1
第一题: 题目大意: 有两个长度为N的序列A和B,在A和B中各任取一个数相加可以得到N2个和,求这N2个和中最小的N个. 解题过程: 1.这题是刘汝佳<<训练指南>>上的一道经 ...
- 变量声明提升 Vs. 函数声明提升
1. 变量声明提升 先看以下代码: 1)var in_window = "a" in window; console.log(in_window); 2)var in_window ...
- redhat6.4上安装mysql
1.挂载操作系统介质 [root@server- ~]# mkdir -p /media/dvd [root@server- ~]# -20130130.0-Server-x86_64-DVD1.is ...
- 将从数组中取到的字符串赋值给了UIImage导致的错误
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantStr ...
- HTTP协议,操作方法的幂等、安全性
google了一些中文的资料, 基本了解了幂等是怎么回事儿. 备忘一下. PUT,DELETE操作是幂等的.所谓幂等是指不管进行多少次操作,结果都一样.比如我用PUT修改一篇文章,然后在做同样的操作, ...
- 用CSS定义每段首行缩进2个字符 转
应该遵循w3c所制定的html/xhtml标准来使用tag和编写网页.如果你对此不太了解,可以到w3c的网站www.w3.org去找相关资料,或者买一本xhtml的书(注意不要买过时的html的书,尽 ...
- 运用Fluxion高效破解WiFi密码
Fluxion是一个无线破解工具,这个工具有点像是Linset的翻版.但是与Linset比较起来,它有着更多有趣的功能.目前这个工具在Kali Linux上可以完美运行. 工作原理 1.扫描能够接收到 ...
- js:setTimeout 与 setInterval 比较
在javascript中有两个非常有用的函数:setTimeout 和setInterval ,都是定时器:但是两者存在着一些区别: 1. setTimeout函数 用法:setTimeout(fn, ...
- Python培训12期-day2作业-购物车
#!/usr/bin/env python import sys import os import getpass 商品={ '图书': { "<Puppet实战>": ...
- ps esc 问题
最近经常发现esc键突然变得不能用了.主要是使用搜狗输入法时使用esc键取输错的字,因此还以为是搜狗的问题,后来突然想到可能是因为打开某个软件导致esc不能用,最后发现居然是ps,在网上查了后发现很多 ...