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. 理解CSS3中的background-size(对响应性图片等比例缩放)

    理解CSS3中的background-size(对响应性图片等比例缩放) 阅读目录 background-size的基本属性 给图片设置固定的宽度和高度的 固定宽度400px和高度200px-使用ba ...

  2. linux下安装python环境

    1.linux下安装python3 a. 准备编译环境(环境如果不对的话,可能遇到各种问题,比如wget无法下载https链接的文件) yum groupinstall 'Development To ...

  3. IIS服务器运行一段时间后卡死,且无法打开网站(IIS管理无响应,必须重启电脑)

    问题描述: 公司希望使用IIS配合网站显示一些订单跟进的情况并展示出来,所以我们在一台演示的Win7 Pro电脑上安装了IIS,但使用了一段时间后发现每过几天页面就无法正常访问了,而且打开IIS管理器 ...

  4. Sturts2的action不执行任何方法的原因

    今天用<s:url action="xxx">调用action的时候出现了一个“异常”, action里的任何方法都没有执行,直接返回success,而且没有任何报错. ...

  5. eclipse工具背景色模板-程序员保护好自己的眼睛

    做为coder,要保护好自己的眼睛,eclipse 强烈推荐 Eclipse Color Theme插件,该插件包含多种当前流行的主题选择. 安装方法: 安装方法:1.先安装一个Eclipse Col ...

  6. explain mysql的type字段,索引的类型

    4.type这列很重要,显示了连接使用了哪种类别,有无使用索引.从最好到最差的连接类型为const.eq_reg.ref.range.indexhe和ALL (1).system这是const联接类型 ...

  7. php综合应用

    php面试题之五--PHP综合应用(高级部分) 五.PHP综合应用 1.写出下列服务的用途和默认端口(新浪网技术部) ftp.ssh.http.telnet.https ftp:File Transf ...

  8. gcc 4.8.3 install centos

    http://blog.csdn.net/xlx921027/article/details/17382643

  9. python del 注意点

    >>> del a[:] >>> a [] del也可以用于删除整个变量: >>> >>> del a 之后再引用名称 a 将会 ...

  10. 关于tableView的错误提示

    WARNING: Using legacy cell layout due to delegate implementation of tableView:accessoryTypeForRowWit ...