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的更多相关文章

  1. 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 ...

  2. Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

    以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...

  3. 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 ...

  4. 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 ...

  5. [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 ...

  6. apt-get remove, apt-get autoremove和aptitude remove的区别

    这篇文章的图片链接发生了问题,无法正常查看图片,所以我在CSDN转载一下,特此声明. apt-getremove的行为我们很好理解,就是删除某个包的同时,删除依赖于它的包,例如:A依赖于B, B依赖于 ...

  7. 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

  8. 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 ...

  9. Search in Sorted Array,Search in Rotated Sorted Array,Search in Rotated Sorted ArrayII

    一:Search in Sorted Array 二分查找,可有重复元素,返回target所在的位置,只需返回其中一个位置,代码中的查找范围为[low,high),左闭右开,否则容易照成死循环. 代码 ...

随机推荐

  1. mybatis.net insert 返回主键

    mybatis insert语句 <insert id="Add" parameterClass="UserInfo" resultClass=" ...

  2. IT公司100题-tencent-打印所有高度为2的路径

    问题描述: 打印所有到叶子节点长度为2的路径  10  /  \ 6   16 / \   / \ 4 8  14 18   / \    / \    \ 2  5  12 15 20 / 11   ...

  3. 链式编程中的next()和end()

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  4. CSS 第四天 多重背景 变形 过渡

    背景原点:background-origin 图片起始位置 border-box包括边框 padding-box边框内 content-box 内容内 **background-repeat 为no- ...

  5. C++ 构造与析构函数

    这两个概念并不对等,构造函数可以完全控制成员构造过程(通过初始化列表),析构函数准确说应该叫析构之前被调用的函数 一般不应该手动调用析构函数:栈区对象会自动析构,堆区也是在delete的时候析构 有一 ...

  6. 解决如何监听Activity切换

    本篇博文在我之前的博文中已经提到了,但是监听Activity切换又可以作为一个单独的内容来叙述,因此这里又单独拿了出来进行赘述. Activity的切换无非有两种,第一种:启动或者创建一个新的Acti ...

  7. ZeroMQ - 三种模型的python实现

    ZeroMQ是一个消息队列网络库,实现网络常用技术封装.在C/S中实现了三种模式,这段时间用python简单实现了一下,感觉python虽然灵活.但是数据处理不如C++自由灵活. 1.Request- ...

  8. iOS开发ARC内存管理

    本文的主要内容: ARC的本质 ARC的开启与关闭 ARC的修饰符 ARC与Block ARC与Toll-Free Bridging ARC的本质 ARC是编译器(时)特性,而不是运行时特性,更不是垃 ...

  9. Selenium定位一 --单个元素定位方法

    Selenium-Webdriver 提供了强大的元素定位方法,支持以下三种方法. 单个对象的定位方法 多个对象的定位方法 层级定位 定位单个元素在定位单个元素时,selenium-webdriver ...

  10. ✡ leetcode 158. Read N Characters Given Read4 II - Call multiple times 对一个文件多次调用read(157题的延伸题) --------- java

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...