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
.
这道题特别简单,其实关于链表的题都没有什么技巧,就是一堆的指针指来指去.
/**
* 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) {
ListNode *pre,*now,*Head;
if(!head||!head->next)return head;
Head=new ListNode(-);
Head->next=head;
pre=Head;
now=head;
while(now&&now->next)
{
if(now->val == now->next->val)
{
while(now->next && now->val == now->next->val)
{
now=now->next;
}
pre->next=now->next;
now=now->next;
}
else
{
pre=now;
now=now->next;
}
}
head=Head->next;
delete(Head);
return head;
}
};
Remove Duplicates from Sorted List II——简单的指针问题的更多相关文章
- 【链表】Remove Duplicates from Sorted List II(三指针)
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- 48. Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that each ...
- 【LeetCode练习题】Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...
- LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II
1. Remove Duplicates from Sorted List 题目链接 题目要求: Given a sorted linked list, delete all duplicates s ...
随机推荐
- Kerberos的白银票据详解
0x01白银票据(Silver Tickets)定义 白银票据(Silver Tickets)是伪造Kerberos票证授予服务(TGS)的票也称为服务票据.如下图所示,与域控制器没有AS-REQ 和 ...
- (转)Xsl 的Webshell(aspx)版本
关于使用xsl的webshell以前已经有人发过了,比如aspx的一个webshell如下: <%@ Page Language="C#" Debug="true& ...
- Java之JNI的介绍与应用20170622
/*************************************************************************************************** ...
- 《剑指offer》— JavaScript(2)替换空格
替换空格 题目描述 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 实现 ...
- pdf 下载整理
pdf下载整理: using System; using System.Collections.Generic; using System.Linq; using System.Web; using ...
- 题解【bzoj3529 [SDOI2014]数表】
Description \(T\) 组询问,定义 \(F(n)=\sum\limits_{d|n}d\).每次给出 \(n,m,a\) 求 \[\sum\limits_{i=1,j=1,F(\gcd( ...
- 删除rabbitmq中持久化的队列和数据
在windows中的rabbitmq安装目录中的/sbin目录下: rabbitmqctl.bat stop_app rabbitmqctl.bat reset rabbitmqctl start_a ...
- 前端PHP入门-002-安装WAMP的集成环境md
> 第一次讲PHP,让我感觉还是满好玩的,一种新的知识的学习,需要我们努力! > 这次PHP课程计划是15天快速入门的课程! 只是单独的讲PHP语言,不涉及很深的内容,只是想让web前端的 ...
- '0','\0',NULL,EOF的区别
要看是不是一个东西,打印一下即可 printf("%d %d %d %d\n",'0','\0',NULL,EOF); 输出: 48 0 0 -1 结论: '\0'与NULL 都是 ...
- HTML跳转
<!DOCTYPE html><HTML><HEAD> <TITLE> HolyMirror </TITLE> <meta http- ...