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. [转] "self = [super init]"的解释与潜藏bug

    Objective-C的推荐init方法写法如下: - (id) init { if(self = [super init]) { //为子类增加属性进行初始化 } return self; } 这里 ...

  2. golang 文件操作

    package main import (     "bytes"     "fmt"     "io"     "os" ...

  3. (转)Predictive learning vs. representation learning 预测学习 与 表示学习

    Predictive learning vs. representation learning  预测学习 与 表示学习 When you take a machine learning class, ...

  4. malloc原理和内存碎片[转]

    当一个进程发生缺页中断的时候,进程会陷入内核态,执行以下操作: 1.检查要访问的虚拟地址是否合法 2.查找/分配一个物理页 3.填充物理页内容(读取磁盘,或者直接置0,或者啥也不干) 4.建立映射关系 ...

  5. photoshop,黑白转彩色单色

    方法一: 打开 色相/饱和度 面板,勾选上 着色, 然后调节 明度 滑块. 方法二: 前面方法的缺点是会将修改直接塌陷到图层,如果想不塌陷,可以使用色相蒙板: 同样勾选 着色

  6. sokite

    <?php interface Proto { //连接 function conn($url); //发送get请求 function get(); //发送post请求 function p ...

  7. [JBoss] - 解决URI提交时乱码问题

    JBoss 7 AS解决url提交数据乱码的问题: 打开jboss-as-7.1.1.Final\standalone\configuration\standalone.xml文件,在<exte ...

  8. 建工发债sql

    管理费用 为了得到科目名称,只好再从外面写一层 select a.*, (select b.subjname from bd_accsubj b where b.subjcode=a.scode an ...

  9. 彻底解决phpcms v9升级后,文章发布出现: Mysql 1267错误:MySQL Error : Illegal mix of collations 解决办法

    彻底解决phpcms v9升级后,文章发布出现: MySQL Query : SELECT * FROM `withli_a`.`v9_keyword` WHERE `keyword` = '吼吼' ...

  10. AMap公交线路查询

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta http ...