题意:删掉单链表里重复的节点,如:
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

思路:真的真的是很简单的题啊,但是有个陷阱,也怪自己练的太少,都忘记了这个。思路其实就是判断如果p->val == p->next->val就删掉p->next节点,如果新的p->next的值与p的值不相等则往后移动。这里的判断条件有两个,

一个是p->val != p->next->val,一个是p->next != NULL。

代码:

ListNode* deleteDuplicates(ListNode* head)
{
ListNode* p = head;
if(head == NULL || head->next == NULL)
return head;
while(p->next != NULL)
{
if(p->val == p->next->val)
{
ListNode* temp = p->next;
p->next = p->next->next;
free(temp);
}
if(p->next != NULL && p->val != p->next->val)
{
p = p->next;
}
}
return head;
}

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

  1. Leetcode-83 Remove Duplicates from Sorted List

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

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

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

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

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

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

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

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

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

  6. Remove Duplicates from Sorted List II

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

  7. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  8. 【leetcode】Remove Duplicates from Sorted Array II

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

  9. 26. Remove Duplicates from Sorted Array

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

随机推荐

  1. underscore的bind和bindAll方法

    bind方法和bindAll方法都是用来设定函数的this值的,区别是调用方式不同. var xiaoming = { say:function(){ console.log('I am xiaomi ...

  2. shell脚本应用

    解析乱的日志文件到临时文件中,然后用awk  1004  cd /usr/local  1005  ll  1006  cd pttmsg/  1007  ll  1008  cd msgbin-2/ ...

  3. HDU 2639 01背包求第k大

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. 迅雷Bolt的ClipSubBindBitmap函数特别说明

    因为在工作中基于迅雷Bolt开发的是IM产品,需要实现自定义用户头像的功能. 但Bolt中对图像的默认拉伸锯齿效果非常明显,所以自己实现了图像拉伸函数,代码已共享,具体可查看:<迅雷Bolt图像 ...

  5. Java 处理 XML 的三种主流技术及介绍

    Java 处理 XML 的三种主流技术及介绍 原文地址:https://www.ibm.com/developerworks/cn/xml/dm-1208gub/ XML (eXtensible Ma ...

  6. javascript中Date使用总结(转)

    //全局函数 Date //Date 类的静态方法 Date.parse Date.UTC //Date 对象的建立方法 new Date() new Date(毫秒数) new Date(标准时间格 ...

  7. HTTP缓存原理

    http的缓存分为强制缓存和对比缓存,两者的区别在于,强制缓存只要设置的时间不过期,就可以直接拿去用,而不用向服务器再一次发送请求.而对比缓存不管缓存是否有效,都需要向服务器发送请求. 其过程如下: ...

  8. lesson 1

    1.当前只是开始接触,安装并开始熟悉Visual Studio 的操作界面及基本设置 2.学习了新建项目,简单的hello world,及对颜色的更改.

  9. Ubuntu下hadoop集群搭建

    --修改IP地址(克隆镜像后可修改可不修改) http://jingyan.baidu.com/article/e5c39bf5bbe0e739d7603396.html -------------- ...

  10. doxygen使用

    前言 下面主要讲解linux下Doxygen命令行实现html文档生成的操作,当然也有界面版本操作方式,linux下安装doxygen-gui即可通过doxywizard开启界面操作,windows下 ...