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.

这题比较简单,用两个指针,一个cursor遍历链表用,一个stick指向上一个不重复的node,这样每次遍历时如果cursor跟stick不一样的话就把stick的next指向cursor,然后更新stick,这样一直到遍历结束,还有最后一步很容易遗漏,就是把stick指向NULL,因为此时stick应该是新链表的尾巴。

这个方法能适用是因为链表是有序的。

ListNode *deleteDuplicates(ListNode *head) {
if (!head) return NULL; ListNode *stick = head, *cursor = head->next;
while (cursor) {
if (cursor->val == stick->val) {
cursor = cursor->next;
continue;
}
else {
stick->next = cursor;
stick = cursor;
}
cursor = cursor->next;
}
stick->next = NULL;
return head;
}

[LeetCode] Remove Duplicates from Sorted List的更多相关文章

  1. LeetCode:Remove Duplicates from Sorted List I II

    LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ...

  2. LeetCode:Remove Duplicates from Sorted Array I II

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

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

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

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

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

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

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

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

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

  7. [Leetcode] Remove Duplicates From Sorted Array II (C++)

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

  8. Leetcode: Remove Duplicates from Sorted List II 解题报告

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

  9. [LeetCode]Remove Duplicates from Sorted Array题解

    Remove Duplicates from Sorted Array: Given a sorted array, remove the duplicates in place such that ...

  10. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

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

随机推荐

  1. python字符串函数

  2. ssh免密码登入

    通常做许多事情(git puh/脚本等等),不停输入密码是件很不愉快的事情,破解如下: http://www.linuxproblem.org/art_9.html 1. 生成rsa密钥 ssh-ke ...

  3. django 1.7+ default_permissions

    由于做Caption要做权限设计.在核心类的设计的时候需要做好权限的基础设计.django 1.7+以后 django.db.modes新增特性 default_permissions,官方文档语焉不 ...

  4. 2016 Multi-university training contest

    day 1 A 给G,w(e)1M(diff),|V|100K,|E|1M,求 MST MST上任意两点间距离的期望 显然MST唯一 E(dis(u,v))可以通过计算每条边的贡献加出来 B n个并行 ...

  5. [k]css盒模型

    box-sizing :  content-box || border-box || inherit 1.content-box:此值为其默认值.元素的宽度/高度(width/height)等于元素边 ...

  6. Best Meeting Point

    Total Accepted: 701 Total Submissions: 1714 Difficulty: Medium A group of two or more people wants t ...

  7. appium + python 环境搭建

    所需:JDK.Android SDK.Appium服务程序.Appium客户端程序 1. 安装最新的JDK,并配置环境变量. JAVA_HOME=C:\Program Files (x86)\Java ...

  8. Pythonj~module

    常数,变量 特殊变量:__xxx__  可以被直接引用 private函数:_xxx __xxx__ 外部不需要引用的函数全部定义成private,只有外部需要引用的函数才定义为public. 导入 ...

  9. monitor system

    #!/bin/bash # #Snapshot_Stats - produces a report for system stats # This report will mail to root. ...

  10. 用window.print()打印指定div里面的内容(转载的)

    用window.print()打印指定div里面的内容 今天客户让添加个打印证照功能,直接用window.print()打印的是整个页面,而用以下方法就可以只打印证明了 <!--window.p ...