题意:删掉单链表里重复的节点,如:
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. webpack 3.8 使用 extract-text-webpack-plugin 3.0 抽取css失败:You may need an appropriate loader to handle this file type.

    webpack 3.8.1 使用 extract-text-webpack-plugin 3.0.2 抽取css时失败,报错: ERROR in ./src/static/style/localTim ...

  2. How do I see what character set a database / table / column is in MySQL?

    Q: How do I see what the character set that a MySQL database, table and column are in? Is there some ...

  3. E. Sonya and Ice Cream(开拓思维)

    E. Sonya and Ice Cream time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  4. java的GC与内存泄漏

    从诞生至今,20多年过去,Java至今仍是使用最为广泛的语言.这仰赖于Java提供的各种技术和特性,让开发人员能优雅的编写高效的程序.今天我们就来说说Java的一项基本但非常重要的技术内存管理 了解C ...

  5. [Usaco2015 dec]Max Flow 树上差分

    [Usaco2015 dec]Max Flow Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 353  Solved: 236[Submit][Sta ...

  6. ViewData和ViewBag的那些事

    既然结论是“共享着相同的数据”,那我们就证实一下吧. 看来结论是正确的. 去查看定义,发现他们的类型是不一样的,ViewData是ViewDataDictionary,ViewBag是dynamic. ...

  7. iOS 全局变量设置的几种方式~

    在iOS开发过程中关于全局变量的几个方法 1. 在APPDelegate中声明并初始化全局变量.AppDelegate可以在整个应用程序中调用,在其他页面中可以使用代码段获取AppDelegate的全 ...

  8. centos yum 安装 mysql

      centos7下使用yum安装mysql 时间:2015-03-07 21:26:20      阅读:87445      评论:0      收藏:1      [点我收藏+] 标签: Cen ...

  9. css做中划线与文字排版

    html: <div class="spilt">    <span class="left"></span>    < ...

  10. 设置查看java的源程序

    1.点 “window”-> "Preferences" -> "Java" -> "Installed JRES" 2. ...