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. ajax基础了解

    使用Ajax的最大优点,就是能在不更新整个页面的前提下维护数据.这使得Web应用程序更为迅捷地回应用户动作,并避免了在网络上发送那些没有改变过的信息.AJAX即“Asynchronous JavaSc ...

  2. tcp三次握手与四次挥手

  3. webrtc第二篇 聊天室

    聊天室模型不一样考虑的问题也不一样 1.websocket文本聊天 * step1 : 向聊天室所有用户(不包括该用户自己)发送当前用户上线信息.客户端用户栏回添加此用户 * step2 : 将该用户 ...

  4. 字符串匹配算法——KMP算法

    处理字符串的过程中,难免会遇到字符匹配的问题.常用的字符匹配方法 1. 朴素模式匹配算法(Brute-Force算法) 求子串位置的定位函数Index( S, T, pos). 模式匹配:子串的定位操 ...

  5. LAMP平台部署及应用

    环境:http://www.cnblogs.com/zzzhfo/p/5925786.html  http://www.cnblogs.com/zzzhfo/p/5934630.html 1.LAMP ...

  6. 进军swift

    swift中文文档网站 http://letsswift.com/category/swiftguide/language-guide/ Swift的优缺点 , 来自珍妮讲解~~ 优点1.简洁(不是说 ...

  7. python del 注意点

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

  8. word20161203

    B-channel / B 信道 B-ISDN, broadband integrated services digital network / 广播综合业务数字网络 backbone router  ...

  9. Android学习之路书籍推荐

    Android开发书籍推荐:从入门到精通系列学习路线书籍介绍 JAVA入门书籍: < Introduction to java programming > < Core java & ...

  10. C++虚函数、虚继承、对象内存模型(转)

    参考:http://blog.csdn.net/hxz_qlh/article/details/14633361 需要注意的是虚继承.多重继承时类的大小.