Remove Duplicates from Sorted List II [LeetCode]
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]的更多相关文章
- Remove Duplicates from Sorted Array II [LeetCode]
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- Remove Duplicates from Sorted Array II ——LeetCode
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- Remove Duplicates from Sorted Array II leetcode java
题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For e ...
- Remove Duplicates from Sorted List II leetcode java
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- Remove Duplicates from Sorted List II ——LeetCode
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 82. 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II)
82. 删除排序链表中的重复元素 II 82. Remove Duplicates from Sorted List II 题目描述 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中没有 ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【LeetCode练习题】Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
随机推荐
- VMware ESXI5.0的安装配置 zz
http://www.hotxf.com/thread-297-1-1.html 1, Vmware ESXI 光盘一张文件大小290M,本教程是以 5.0为案例. 2, 所需要安装的操作 ...
- c++ 动态分配二维数组 new 二维数组
#include "stdafx.h" #include <iostream> using namespace std; int _tmain(int argc, _T ...
- left join 等连接查询遇到同名字段覆盖问题
可以在查询时给字段赋别名,但是需要注意以下:*的位置要在最前面,放在其他地方都会出错.这种写法同名覆盖的字段还在,然后在*的后面加上别名字段,已经可以满足所有需求了 SELECT *,r.id as ...
- VIM如何将全部内容复制并粘贴到外部
ubuntu默认安装的vim是不支持系统剪切.粘贴版的,需要执行以下安装:sudo apt-get install vim-gnome 注意要让vim支持系统粘贴板,首先执行sudo apt-get ...
- linux设置tomcat开机自启动
本文假设jdk环境安装成功,如何安装JDK请参考这个链接: http://www.cnblogs.com/yoyotl/p/5395208.html 1. 下载apache的安装包,例如本例下载了ap ...
- application 网站计数器
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- JAVA排序--[冒泡排序]
package com.array; public class Sort_MaoPao { /** * 项目名称:冒泡排序 * 项目要求:用JAVA对数组进行排序,并运用冒泡排序算法 * 作者:Sev ...
- poj 1066 线段相交
链接:http://poj.org/problem?id=1066 Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- Visual C#两分钟搭建BHO IE钩子(转)
摘自:http://www.cnblogs.com/mvc2014/p/3776054.html 微软在1997年正式推出Browser Helper Object (BHO), 使程序员能够更好的对 ...
- canvas背景透明
var can=document.getElementById("canv"); c=can.getContext("2d"); c.globalAlpha=. ...