https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/

题目:

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.

思路:

需要两个指针,a用来纪录新链表,b用来遍历老链表,因为只有删除操作,需要一个flag标记ab段要不要删除。此外要注意头节点的处理。

AC代码:

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head==NULL)
return NULL;
ListNode* newhead=new ListNode(); // to handle head
newhead->next=head;
ListNode* a=newhead,*b=newhead->next;
int now=head->val;
bool occur_flag=false;
while(b->next!=NULL){
if(b->next->val==now){
occur_flag=true;
}
else{
if(occur_flag==true){
a->next=b->next; //delete duplicate numbers
b=a; //update pointers
}
else{
a=b;
}
occur_flag=false;
now=b->next->val;
}
b=b->next;
}
if(occur_flag==true){
a->next=b->next; //delete duplicate numbers
b=a; //update pointers
}
else{
a=b;
}
return newhead->next;
}
};

LeetCode(82)题解: Remove Duplicates from Sorted List II的更多相关文章

  1. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

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

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

  3. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

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

  4. 【LeetCode】080. Remove Duplicates from Sorted Array II

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

  5. leetcode笔记:Remove Duplicates from Sorted Array II

    一.题目描写叙述 二.解题技巧 这道题和Remove Duplicates from Sorted Array这道题是相似的.仅仅只是这里同意出现反复的数字而已,能够採用二分搜索的变种算法.仅仅只是增 ...

  6. LeetCode(26)题解:Remove Duplicates from Sorted Array

    https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...

  7. LeetCode OJ 80. Remove Duplicates from Sorted Array II

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

  8. LeetCode OJ:Remove Duplicates from Sorted Array II(移除数组中的重复元素II)

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

  9. LeetCode(83)题解: Remove Duplicates from Sorted List

    https://leetcode.com/problems/remove-duplicates-from-sorted-list/ 题目: Given a sorted linked list, de ...

随机推荐

  1. 最短Hamilton路径-状压dp解法

    最短Hamilton路径 时间限制: 2 Sec  内存限制: 128 MB 题目描述 给定一张 n(n≤20) 个点的带权无向图,点从 0~n-1 标号,求起点 0 到终点 n-1 的最短Hamil ...

  2. openxml的视频教程

    http://msdnwebcast.net/webcast/0/1980/#1032360142 最近发现的一个openxml的视频教程

  3. Day 15 python 之 列表、元组、字典

    基础: #! /usr/bin/env python # -*- coding: utf-8 -*- # __author__ = "DaChao" # Date: 2017/6/ ...

  4. GNOME 3.x下安装配置小企鹅输入法框架及SunPinYin插件

    fcitx 小企鹅输入法框架已经越来越成熟,并且具备极高的性能,配合 Sun PinYin 智能输入法就和 Windows 下的搜狗百度等输入法几乎无二了.事实上,现在Linux版本的搜狗输入法正是基 ...

  5. Android调起地图导航

       想要使用导航功能可以使用各个地图的开放平台集成导航模块,如果不想集成也可以调起相关app导航 调起其他app首先得使用到该app包名,先贴出来 public final static Strin ...

  6. tomcat7.0.55配置单向和双向HTTPS连接

    HTTPS配置中分为单向连接和双向连接,单向连接只需要服务器安装证书,客户端不需要,双向连接需要服务器和客户端都安装证书 下面的配置都没有用CA签名来配置,都不能用于生产环境,实际配置中是需要CA的, ...

  7. const T、const T*、T *const、const T&、const T*& 的区别

    原文地址: http://blog.csdn.net/luoweifu/article/details/45600415 这里的T指的是一种数据类型,可以是int.long.doule等基本数据类型, ...

  8. Codeforces Round #321 (Div. 2) Kefa and Dishes 状压+spfa

    原题链接:http://codeforces.com/contest/580/problem/D 题意: 给你一些一个有向图,求不超过m步的情况下,能获得的最大权值和是多少,点不能重复走. 题解: 令 ...

  9. Codechef FNCS Chef and Churu

    Disciption Chef has recently learnt Function and Addition. He is too exited to teach this to his fri ...

  10. JDBC工具类 访问数据库 增删改查CRUD的通用方法

    1 package com.zmh.util; 2 3 import java.sql.*; 4 import java.util.ArrayList; 5 import java.util.Hash ...