闲话链表(四) leetcode之Remove Duplicates from Sorted List I、II
来源:互联网 发布:mac app ui设计 编辑:程序博客网 时间:2024/05/16 09:50
leetcode出处:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/
https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
问题一:
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
分析:这道题有很多解法,我的解法是找到满足条件的节点插入新的链表中。具体描述如下:fakehead是一个新的链表的头结点,每次我用指针cur遍历原链表L,若cur->next为空或者cur->val != cur -> next -> val 说明cur所指节点可以拿来插入。千万不要忘记了cur->next为空的情况,也就是cur是最后一个节点,这种情况下cur必然是可以插入的。之后将cur摘取下来,以尾插法插入fakehead作为头结点的链表中。
(有人考虑到是否要释放内存空间,对于重复的节点我们是否要把它delete掉,我个人觉得这种考虑是正确的,但是leetcode OJ 上并没有针对题目给出main函数,所以链表是否在堆空间上开辟的并不确定。我认为这种情况下贸然用delete是不安全的,在我的认知里,delete栈上的空间这个行为是未定义的。如果我的程序链表是在堆上建立,我会非常乐意用delete释放所占用内存空间)
<span style="font-size:18px;">#include <iostream>using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};class Solution {public: ListNode *deleteDuplicates(ListNode *head) { ListNode *fakehead = new ListNode(-1); ListNode *cur = head; ListNode *p = fakehead; ListNode *q; while(cur) { if(cur -> next && cur -> val == cur -> next -> val) cur = cur -> next; //若cur的下一个节点为空,或者cur的val不等于下一个节点的val,这个cur就是我们想要的节点 else{ //记录下一个要处理的节点 q = cur -> next; //尾插法 p -> next = cur; cur -> next = NULL; p = cur; cur = q; } } return fakehead -> next; }};</span>
问题二:
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.
分析:沿用问题一的思路,仍然是找到满足条件的节点摘取下来插入到新的链表中。这里与问题一不同的是,如果一个节点重复,那么这个节点以及后续所有重复节点都直接舍弃。
<span style="font-size:18px;">#include <iostream>using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};class Solution {public: ListNode *deleteDuplicates(ListNode *head) { ListNode *fakehead = new ListNode(-1); ListNode *cur = head; ListNode *q; ListNode *p = fakehead; while(cur) { //若当前节点是满足条件的节点 if(cur -> next == NULL || cur -> val != cur -> next -> val) { q = cur -> next; //尾插法 p -> next = cur; cur -> next = NULL; p = cur; cur = q; // cur更新为下一个可能的节点 } //若当前节点已经发现有重复元素,则放弃当前节点及其后续重复元素 else { while(cur && cur -> next && cur -> val == cur -> next -> val) { cur = cur -> next; } cur = cur -> next; } } return fakehead -> next; }};</span>
- 闲话链表(四) leetcode之Remove Duplicates from Sorted List I、II
- 【LeetCode】Remove Duplicates from Sorted List I && II
- 【LeetCode】Remove Duplicates from Sorted List I && II
- [Leetcode] Remove Duplicates from Sorted List I | II
- LeetCode Solutions : Remove Duplicates from Sorted List I & II
- leetcode--83&82 Remove Duplicates from sorted list I&II
- 082&083 Remove Duplicates from Sorted List I II [Leetcode]
- Remove Duplicates from Sorted List I II -- leetcode
- [Leetcode] #82#83 Remove Duplicates from Sorted List I & II
- LeetCode:Remove Duplicates from Sorted List I & II
- Remove Duplicates from Sorted List I II
- Remove Duplicates from Sorted List I,II
- Remove Duplicates from Sorted List I II
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- 我叫程序员
- Delphi XE7中新并行库
- C#设计模式之简单工厂模式
- 移植qt库到arm开发板上 并测试 简单qt_ARM程序
- 2b手机的艰辛刷机
- 闲话链表(四) leetcode之Remove Duplicates from Sorted List I、II
- poj 1321 棋盘问题(dfs)
- 【白话经典算法系列之八】 MoreWindows白话经典算法之七大排序总结篇
- java自带线程池和队列详细讲解
- cocos2dv3 new GUI Usage of Container
- A+B
- C++的cout高阶格式化操作
- SQL语句中CASE WHEN的使用实例
- Archlinux命令之软件卸载