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),左闭右开,否则容易照成死循环. 代码 ...
随机推荐
- new对象时,类名后加括号与不加括号的区别
[1]默认构造函数 关于默认构造函数,请参见随笔<类中函数> 请看测试代码: 1 #include <iostream> 2 using namespace std; 3 4 ...
- autotools工具使用记录
参考 http://blog.chinaunix.net/uid-25100840-id-271131.html http://blog.sina.com.cn/s/blog_4c2bf01a0101 ...
- SharePoint开发——利用CSOM逐级获取O365中SharePoint网站的List内容
博客地址:http://blog.csdn.net/FoxDave 本文介绍如何利用SharePoint客户端对象模型(.NET)逐级获取Office 365网站中List的内容,仅仅是示例,没有 ...
- 在eclipse中安装配置WebDriver
安装好eclipse环境后,下载Selenium jar包 在eclipse中新建一个java project 之后选择新建的工程,将Selenium jar包中的lib文件夹和selenium-ja ...
- jsp无法引入外部.JS或者.CSS文件的有关问题 (转)
<!-- *************JSP代码******************--> <%@ page language="java" pageEncodin ...
- Flex文件结构
一.文件.目录及其作用.project:描述工程信息,如 本工程名称.工程注释.相关工程信息.编译参数等 .flexProperties:记录与Flex本身相关的信息 .actionScriptPro ...
- 安装生物信息学软件-Biopython
其实好多东西装过好多次,然而每次都要翻文档,经常掉进前面掉进过的坑...所以这里重新写一份指南,以防下次再装又忘了(魂淡我并不想再装了啊不要立flag) 1. 安装biopython 1.1 因为bi ...
- 使用GnuRadio+OpenLTE+SDR搭建4G LTE基站(上)
0×00 前言 在移动互联网大规模发展的背景下,智能手机的普及和各种互联网应用的流行,致使对无线网络的需求呈几何级增长,导致移动运营商之间的竞争愈发激烈.但由于资费下调等各种因素影响,运营商从用户获得 ...
- 简单的JS多物体的运动---运动和透明度的变化
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- CI整合Smarty
1.到相应的站点下载smarty模板: 2.将源代码中的libs目录复制到项目的libraries目录下,改名为smarty3.0 3.在项目目录的libraries文件夹内新建文件ci_smarty ...