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.

思路:

简单题,没什么好说的。

class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if(head == NULL)
return NULL; ListNode * ans = head;
ListNode * anscur = ans; //记录答案链表的最后一个指针
ListNode * cur = head->next; //记录判断的是原链表中的哪一个指针
while(cur != NULL)
{
if(anscur->val != cur->val) //只有值变化的时候才把指针接到ans链表后面 节省操作
{
anscur->next = cur;
anscur = anscur->next;
}
cur = cur->next;
}
anscur->next = NULL; //防止最后的指针是重复的,给ans结尾
return ans;
}
};

大神的代码更加简洁, 没有必要新建一个头结点,就用原来的头结点就好。

class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if(NULL == head) return head; ListNode *cur = head, *nxt = head->next;
while (nxt) {
if (cur->val != nxt->val)
cur = cur->next = nxt;
else if (!nxt->next)
cur->next = nxt->next;
nxt = nxt->next;
} return head;
}
};

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

  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(2)

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

  4. 【leetcode】Remove Duplicates from Sorted Array

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

  5. 【leetcode】Remove Duplicates from Sorted List

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

  6. 【leetcode】 Remove Duplicates from Sorted List

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

  7. 【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 ...

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

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

  9. 【Leetcode】Remove Duplicates from Sorted List II

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

随机推荐

  1. [整理]AngularJS学习资源

    https://angular.io/docs/js/latest/(2.0官方网站) http://www.linuxidc.com/Linux/2014-05/102139.htm(Angular ...

  2. java框架

    Dash Reports 1.0发布 Java报表解决方案 http://developer.51cto.com/art/201205/337189.htm http://www.oschina.ne ...

  3. Codeforces Round #302 (Div. 2).C. Writing Code (dp)

    C. Writing Code time limit per test 3 seconds memory limit per test 256 megabytes input standard inp ...

  4. hdu.1010.Tempter of the Bone(dfs+奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  5. 71-IO 流

    JAVA IO 流 一.概括 1.IO(INPUT OUTPUT)流 1.1 IO 流用来处理设备之间的数据传输 1.2JAVA 对数据的操作是通过流的方式 1.3 JAVA 用于操作流的对象都在 I ...

  6. ORACLE连接字符串里每个参数的具体意思

    1.数据库名(db_name):数据库名是存储在控制文件中的数据库的名称.它代表的是数据库也就是所有构成数据库的物理文件的总称.要修改这个名称,只要重建控制文件就行了.2.实例名:实例名指的是用于响应 ...

  7. java 1.7

    http://superuser.com/questions/740064/how-to-install-java-1-7-runtime-on-macos-10-9-mavericks sudo r ...

  8. httpd服务访问控制

    客户机地址限制 通过配置Order.Deny from.Allow from 来限制客户机 allow.deny :先"允许"后"拒绝" ,默认拒绝所有为明确的 ...

  9. 雪峰配置的nginx

  10. Android学习笔记(七)——常见控件

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! Android 给我们提供了大量的 UI控件,下面我们简单试试几种常用的控件. 一.TextView 在布局文 ...