题目链接: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. Linux之apt-get软件管理

    apt-get 用Linux apt-get命令的第一步就是引入必需的软件库,Debian的软件库也就是所有Debian软件包的集合,它们存在互联网上的一些公共站点上.把它们的地址加入,apt-get ...

  2. maven-war-plugin

    Name Type Since Description 默认值 cacheFile File 2.1-alpha-1 包含webapp结构的文件缓存 ${project.build.directory ...

  3. string::substr

    string substr (size_t pos = 0, size_t len = npos) const; #include <iostream> #include <stri ...

  4. MyBatis中<![CDATA[ ]]>的使用

    原文地址:https://www.cnblogs.com/catgatp/p/6403382.html <![CDATA[]]>和转义字符 被<![CDATA[]]>这个标记所 ...

  5. Mybatis关联查询(转载)

    原文地址: http://www.cnblogs.com/xiaolang8762400/p/7399892.html   mybatis 提供了高级的关联查询功能,可以很方便地将数据库获取的结果集映 ...

  6. 34.第一次只出现一次的字符(python)

    题目描述 在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).   两次遍历,第一次存放字 ...

  7. php+大视频文件上传+进度条

    该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开始. 如何分 ...

  8. Thread(简单使用)

    /***thread.c***/#include<stdio.h> #include<stdlib.h> #include<pthread.h> void prin ...

  9. Spring Boot教程(四十一)LDAP来管理用户信息(1)

    LDAP简介 LDAP(轻量级目录访问协议,Lightweight Directory Access Protocol)是实现提供被称为目录服务的信息服务.目录服务是一种特殊的数据库系统,其专门针对读 ...

  10. 一键生成koa/koa2项目

    1. npm install -g koa-generator 安装生成器 2.执行 koa mytest (koa1项目) koa2 koa2test (koa2项目) 3.进入目录 cd koa2 ...