LeetCode:Remove Duplicates from Sorted List I II
LeetCode:Remove Duplicates from Sorted List
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
.
分析:和从数组中移除重复元素那一题一样的思想
/*** 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) {// IMPORTANT: Please reset any member data you declared, as// the same Solution instance will be reused for each test case.if(head == NULL || head->next == NULL)return head;ListNode *index = head, *p = head->next, *pre = head;while(p != NULL){if(p->val != pre->val){index = index->next;index->val = p->val;}pre = p;p = p->next;}p = index->next;index->next = NULL;while(p != NULL){//销毁多余的节点ListNode *tmp = p;p = p->next;delete tmp;}return head;} };
LeetCode: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) {// IMPORTANT: Please reset any member data you declared, as// the same Solution instance will be reused for each test case.if(head == NULL)return head;//为操作方便添加头结点ListNode *addHead = new ListNode(0);addHead->next = head;//index 指向已经保存的最后一个节点ListNode *index = addHead, *p = head;while(p != NULL){if(p->next == NULL || p->val != p->next->val){//当前节点没有重复index = index->next;index->val = p->val;p = p->next;}else{//当前节点有重复,找到当前节点的最后一个副本的下一个元素while(p->next && p->val == p->next->val)p = p->next;p = p->next;}}p = index->next;index->next = NULL;while(p != NULL){//销毁多余的节点ListNode *tmp = p;p = p->next;delete tmp;}head = addHead->next;delete addHead;return head;} };
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3461481.html
转载于:https://www.cnblogs.com/TenosDoIt/p/3461481.html
LeetCode:Remove Duplicates from Sorted List I II相关推荐
- Leetcode OJ: Remove Duplicates from Sorted Array I/II
删除排序数组重复元素,先来个简单的. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates i ...
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...
- [LeetCode] Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...
- LeetCode Remove Duplicates from Sorted List II
题意:给出一个单链表 ,将其中重复的元素删除 思路:在找重复结点时,需要找到其前继结点 代码如下: class Solution {public ListNode deleteDuplicates(L ...
- LeetCode() Remove duplicates from sorted list II
ListNode* dummy = new ListNode(0); //必须要加上 new ListNode(0); 否则有错误. dummy->next = head; head = dum ...
- LeetCode Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- [LeetCode] Remove Duplicates from Sorted List - 链表问题
题目概述: Given a sorted linked list, delete all duplicates such that each element appear only once. For ...
- Remove Duplicates from Sorted Array II -- LeetCode
原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 这道题跟Remove Duplicates ...
- LeetCode 80. Remove Duplicates from Sorted Array II
80. Remove Duplicates from Sorted Array II My Submissions QuestionEditorial Solution Total Accepted: ...
最新文章
- C语言中的输入输出函数
- RoFormerV2:自然语言理解的极限探索
- java 数据库改操作_数据库的插入、修改、删除操作(java实现)
- 0和5(51Nod-1433)
- 让服务器自动从HG版本库中下载代码
- paip.提升效率--数据绑定到table原理和流程Angular js jquery实现
- Docker存储驱动之总览
- 图解设计模式:行为型模式之责任链模式
- java带圈数字,小1,小2
- Firebug 入门指南
- 三角形的几何公式大全_解析几何(椭圆)常见二级结论92条附详细证明
- 热血江湖数据库MYSQL修改_热血江湖数据库修改终极教程-20070823更新
- CANoe如何查看总线负载率?
- BLE蓝牙连接不稳定以及突然断开的原因和解决方法
- IOS 多线程之信号量
- 曾扬言 机器人合法公民_简直荒谬!这个机器人被授予合法公民身份,并公开嘲笑马斯克!...
- 定义一个学生类Student,包含三个属性姓名、年龄、性别, 创建三个学生对象存入ArrayList集合中。 	A:使用迭代器遍历集合。 	B:求出年龄最大的学生,然后将该对象的姓名变为:小猪佩奇。
- intel clear linux 教程,英特尔ClearLinux安装教程
- 面向协议与面向对象的区别
- 电脑开机自检的过程和屏显