题目链接:https://leetcode.com/problems/rotate-list/description/

Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

Input: 1->1->2
Output: 1->2

Example 2:

Input: 1->1->2->3->3
Output: 1->2->3

思路:

  • 有序的链表中删除有重复值的结点。
  • 需要两个指针分别指向链表的第一个结点和第二个结点(如果存在),这两个指针分别为precur; 判断这两个指针所指向结点的值是否相等。
    1. 若相等,则改变指针指向对多余元素进行删除;
    2. 若不等,则pre = cur ; cur = cur->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) {
if (head == nullptr) return head; ListNode *cur = head;
ListNode *pre = nullptr; while ( cur->next != nullptr)
{
pre = cur;
cur = cur->next;
while ( cur != nullptr && pre->val == cur->val)
{
pre->next = cur->next;
cur = cur->next;
} // 应考虑到链表的尾节点为多余元素的情况
if (cur == nullptr)
{
return head;
}
} return head;
}
};

083. Remove Duplicates from Sorted List的更多相关文章

  1. [LeetCode]题解(python):083 - Remove Duplicates from Sorted List

    题目来源 https://leetcode.com/problems/remove-duplicates-from-sorted-list/ Given a sorted linked list, d ...

  2. Java for LeetCode 083 Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  3. 083 Remove Duplicates from Sorted List 有序链表中删除重复的结点

    给定一个排序链表,删除所有重复的元素使得每个元素只留下一个.案例:给定 1->1->2,返回 1->2给定 1->1->2->3->3,返回 1->2- ...

  4. [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  5. [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  6. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  7. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  8. Leetcode-83 Remove Duplicates from Sorted List

    #83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...

  9. Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

随机推荐

  1. Codeforce Round #424

    A 略 B 略 C: 先对Ai数列预处理前缀和 然后把Bi的每个都加一次 最终得到的结果为ans[sum]++; 最后如果有一个ans[sum]>=k即满足所有K个条件就输出(注意!!前缀和要进 ...

  2. Summer training #2

    A:不管有没有负数 一顿操作之后肯定只有正数 又因为A=A-B 所以最大值是一直在减小的 所以一定有结果 B:..一开始以为求min操作数 WA了2发 直接求所有数的GCD如果所有数的GCD都不是1的 ...

  3. 【原】涉及数据库的单元测试-JTeser

    JTeser方法之一:@DbFit 一.maven 依赖项 <dependency> <groupId>org.testng</groupId> <artif ...

  4. react-缓存

    目录结构: 用到缓存的地方: 缓存的方法 清楚缓存

  5. PHP回顾(面向对象)

    类中的成员属性不能够用函数为其赋值.public age = rand(1,100);//这是错误的: __get()   __set()  __isset() __unset() final 用来修 ...

  6. mysql的五大引擎跟优劣之分

    MySQL有多种存储引擎,每种存储引擎有各自的优缺点,可以择优选择使用: MERGE.MEMORY(HEAP).EXAMPLE.ARCHIVE.CSV. · MEMORY存储引擎提供“内存中”表.ME ...

  7. Volatile关键字的两个作用

    1.保证修饰的变量对所有线程的可见性,这里的“可见性”是指当一条线程修改了这个值,新值对于其他线程来说是可以立即得知的. 2.禁止指令重新排序化

  8. Partial Dependence Plot

    Partial Dependence就是用来解释某个特征和目标值y的关系的,一般是通过画出Partial Dependence Plot(PDP)来体现. PDP是依赖于模型本身的,所以我们需要先训练 ...

  9. 0.JQuery学习

    jQuery 教程 jQuery 是一个 JavaScript 库. jQuery 极大地简化了 JavaScript 编程. jQuery 简介 jQuery 库可以通过一行简单的标记被添加到网页中 ...

  10. redis远程连接命令

    redis-cli -h 172.17.0.85 -p 6379 输入密码 auth "1234