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.

给出一个已排序链表,删除所有重复的元素使每一个节点值只出现一次

思路:

1. 定义pCurNode,pNextNode两个节点指针;

2. pCurNode初始化为链表头节点指针;pNextNode初始化为下一节点

3. 判断pCurNode是否有效;

4. 判断相邻两个节点是否相同,如相同删除后一个相同的节点(其实就是将pCurNode的next指针指向删除节点的下一节点即可),再循环比较原来删除节点后的节点值是否相同。如(1, 1)、(1,1, 1);

5. 如相邻两个节点不同,则将当前节点指针后移一个节点;

6. 重复3、4、5;

class Solution
{
public:
ListNode* deleteDuplicates(ListNode* head)
{
ListNode* pCurNode = head;
ListNode* pNextNode = nullptr;
while (pCurNode)
{
pNextNode = pCurNode->next;
if (pNextNode&&pNextNode->val == pCurNode->val)
{
pCurNode->next = pNextNode->next;
}
else
{
pCurNode = pNextNode;
}
} return head;
} void Print(ListNode* head)
{
ListNode* pCurNode = head;
while (pCurNode)
{
printf(" %d", pCurNode->val);
pCurNode = pCurNode->next;
} printf("\n");
}
};

【leetcode】 Remove Duplicates from Sorted List的更多相关文章

  1. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  2. 【leetcode】Remove Duplicates from Sorted Array I & II(middle)

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

  3. 【leetcode】Remove Duplicates from Sorted Array

    题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...

  4. 【leetcode】Remove Duplicates from Sorted List

    题目简述 Given a sorted linked list, delete all duplicates such that each element appear only once. For ...

  5. 【leetcode】Remove Duplicates from Sorted List (easy)

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

  6. 【Leetcode】Remove Duplicates from Sorted List in JAVA

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

  7. 【leetcode】Remove Duplicates from Sorted List II (middle)

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

  8. 【Leetcode】Remove Duplicates from Sorted List II

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

  9. 【LeetCode】Remove Duplicates from Sorted List(删除排序链表中的重复元素)

    这道题是LeetCode里的第83道题. 题目描述: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: ...

随机推荐

  1. Google Java编程风格指南

    出处:http://hawstein.com/posts/google-java-style.html 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Comm ...

  2. linux修改mysql密码

    以root为列. 查看文件安装路径whereis mysql   查询运行文件所在路径(文件夹地址) which mysql /usr/bin/mysqld_safe: line 178: 5930 ...

  3. How to create/restore a slave using GTID replication in MySQL 5.6

    MySQL 5.6 is GA! Now we have new things to play with and in my personal opinion the most interesting ...

  4. 反人类的java

  5. PHP MVC简单介绍,对PHP当前主流的MVC做了一个总结

    东抄西抄,对PHP当前主流的MVC做了一个总结PPT. 希望对初学者有点帮助! PHP MVC初步.ppt

  6. Python入门4

    函数 函数其实在日常的编码过程中,你都在使用,比如print().input().len()等函数,只不过这些都是python给你写好的内置函数,供你是用,内置的函数数量有限,想让python为我们做 ...

  7. Cannot get a connection, pool exhausted解决办法

    http://blog.163.com/it_message/blog/static/8892051200908102032653/ 连接池(Tomcat+oracle),运行一段时间后就会出现 Ca ...

  8. 手把手教你ARC——iOS/Mac开发ARC入门和使用

    转载自:http://www.onevcat.com/2012/06/arc-hand-by-hand/ 本文部分实例取自iOS 5 Toturail一书中关于ARC的教程和公开内容,仅用于技术交流和 ...

  9. MySQL concat函数的使用

    MySQL concat函数是MySQL数据库中众多的函数之一,下文将对MySQL concat函数的语法和使用进行说明,供您参考和学习. MySQL concat函数使用方法:CONCAT(str1 ...

  10. 通过宏判断VS编译版本以及系统平台

    MSC_VER 定义编译器的版本.下面是一些编译器版本的_MSC_VER值(参见扩展阅读中的参考文献2的链接) MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio ...