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. windows下的socket网络编程

    windows下的socket网络编程 windows下的socket网络编程 clinet.c 客户端 server.c 服务器端 UDP通信的实现 代码如下 已经很久没有在windows下编程了, ...

  2. Kendo UI

    http://www.cnblogs.com/libingql/category/585455.html http://www.scala-china.net/discuz/forum.php?mod ...

  3. (转)C++0x语言新特性一览

    转自:http://blog.csdn.net/zwvista/article/details/2429781 原文请见http://en.wikipedia.org/wiki/C%2B%2B0x. ...

  4. iOS drawRect 和 awakeFromNib

    今天想在一个view上绘制一个虚线边框,百度到的代码如下: CAShapeLayer *borderLayer = [CAShapeLayer layer]; borderLayer.frame = ...

  5. svn: Commit failed (details follow): svn: Authorization failed

    我的原因是我没有使用账户密码,匿名用户没有写权限,只有只读的权限 修改下svn配置文件中的anon-access=read为anon-access=write 还有一点要注意:选项前面不能留空格,必须 ...

  6. InnoDB O_DIRECT选项漫谈(一)【转】

    本文来自:http://insidemysql.blog.163.com/blog/static/2028340422013671186977/   最近和文件系统内核开发人员做技术交流,对O_DIR ...

  7. java wait()和notify()、notifyAll()

    图见<JAVA并发编程的艺术>P98-101 这三个方法都是java.lang.Object的方法,用于协调多个线程对共享数据的存取,必须在synchronized语句块中使用!这三个方法 ...

  8. Java for LeetCode 225 Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  9. OpenNI结合Unity3D Kinect进行体感游戏开发(转)

    OpenNI结合Unity3D Kinect进行体感游戏开发(转) 楼主# 更多 发布于:2012-07-17 16:42     1. 下载安装Unity3D(目前版本为3.4)2. 下载OpenN ...

  10. 【linux】gcc命令

    来源:http://man.linuxde.net/gcc 语法 gcc(选项)(参数) 选项 -o:指定生成的输出文件: -E:仅执行编译预处理: -S:将C代码转换为汇编代码: -wall:显示警 ...