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

For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

Solution:

     ListNode *deleteDuplicates(ListNode *head) {
if(head == NULL || head->next == NULL)
return head; //remove the head
ListNode * same_node = NULL;
while(head != NULL) {
if(same_node == NULL) {
if(head->next != NULL && head->val == head->next->val){
same_node = head;
head = head->next;
}else {
break;
}
}else {
if(head->val == same_node->val)
head = head->next;
else
same_node = NULL;
}
} if(head == NULL || head->next == NULL)
return head; ListNode * pre = head;
ListNode * same = NULL;
ListNode * current = pre->next;
while(current != NULL) {
if(same == NULL) {
if(current->next != NULL && current->val == current->next->val){
same = current;
pre->next = current->next;
}else{
pre = pre->next;
}
current = current->next;
}else {
if(current->val == same->val){
pre->next = current->next;
current = current->next;
}else {
same = NULL;
}
}
} return head;
}

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

  1. Remove Duplicates from Sorted Array II [LeetCode]

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

  2. Remove Duplicates from Sorted Array II ——LeetCode

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

  3. Remove Duplicates from Sorted Array II leetcode java

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

  4. Remove Duplicates from Sorted List II leetcode java

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

  5. Remove Duplicates from Sorted List II ——LeetCode

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

  6. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  7. LeetCode 82. 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II)

    82. 删除排序链表中的重复元素 II 82. Remove Duplicates from Sorted List II 题目描述 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中没有 ...

  8. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  9. 【LeetCode练习题】Remove Duplicates from Sorted List II

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

随机推荐

  1. Servlet上下文

    Servlet上下文 运行在Java虚拟机的每一个Web应用程序都有一个与之相关的Servlet上下文. Java Servlet API提供了一个ServletContext接口来表示上下文.在这个 ...

  2. 浏览器被hao.360.cn劫持怎么办

    特么的现在互联网太没节操了,一大早发现我的浏览器被hao.360.cn劫持了,弄了好久都没弄好,后来一想可能是因为qvod的原因,这可是哥当年看片的神器啊…… 废话不说: 1,进入:C:\Progra ...

  3. C# 模拟鼠标写字

    经常看到别人拿个四四方方的写字板用笔写字,用C#其实也可以实现,大致思路就是处理鼠标的坐标和Graphics, 代码如下: 首先声明两个全局变量 bool isMouseDown = false; P ...

  4. 加载xib文件

    // Test.xib --编译--> Test.nib // 方式1 NSArray *objs = [[NSBundle mainBundle] loadNibNamed:@"Te ...

  5. tilemap坐标转换

    像素点跟tile的索引之间的转换//从cocos2d-x坐标转换为Tilemap坐标CCPoint GameMap::tileCoordForPosition(CCPoint position){ i ...

  6. C语言单链表实现19个功能完全详解

    谢谢Lee.Kevin分享了这篇文章 最近在复习数据结构,想把数据结构里面涉及的都自己实现一下,完全是用C语言实现的. 自己编写的不是很好,大家可以参考,有错误希望帮忙指正,现在正处于编写阶段,一共将 ...

  7. Android ActionBar以及menu的代码设置样式

    menu部分xml代码 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android= ...

  8. How To Use API Online?

    Example: 在线 java8 api 1.http://docs.oracle.com/javase/8/docs/api/ 2.ctrl+f 3.输入类名! 4.

  9. mongoDB 类型参考表

    $type操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果. MongoDB 中可以使用的类型如下表所示: 参考资料:http://www.runoob.com/mongodb/mong ...

  10. tomcat字符,文档,数据库配置

    修改tomcat目录下conf目录下的server.xml tomcat容器的解码配置 URIEncoding="UTF-8" <Connector port="8 ...