LeetCode(82)题解: Remove Duplicates from Sorted List II

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代码:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* deleteDuplicates(ListNode* head) {
12         if(head==NULL)
13             return NULL;
14         ListNode* newhead=new ListNode(0);  // to handle head
15         newhead->next=head;
16         ListNode* a=newhead,*b=newhead->next;
17         int now=head->val;
18         bool occur_flag=false;
19         while(b->next!=NULL){
20             if(b->next->val==now){
21                 occur_flag=true;
22             }
23             else{
24                 if(occur_flag==true){
25                     a->next=b->next;  //delete duplicate numbers
26                     b=a;    //update pointers
27                 }
28                 else{
29                     a=b;
30                 }
31                 occur_flag=false;
32                 now=b->next->val;
33             }
34             b=b->next;
35         }
36         if(occur_flag==true){
37             a->next=b->next;  //delete duplicate numbers
38             b=a;    //update pointers
39         }
40         else{
41             a=b;
42         }
43         return newhead->next;
44     }
45 };
时间: 2024-11-11 18:48:47

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

leetcode || 82、Remove Duplicates from Sorted List II

problem: 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, retur

Leetcode 线性表 Remove Duplicates from Sorted Array II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array II Total Accepted: 10649 Total Submissions: 35325 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorte

Leetcode 线性表 Remove Duplicates from Sorted List II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted List II Total Accepted: 10702 Total Submissions: 43714 Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from th

leetcode笔记:Remove Duplicates from Sorted Array II

一.题目描述 二.解题技巧 这道题和Remove Duplicates from Sorted Array这道题是类似的,只不过这里允许出现重复的数字而已,可以采用二分搜索的变种算法,只不过加入了剔除和第一个元素相同的元素的过程.另一个思路是加入一个变量,用于记录元素出现的次数.这题因为是已经排序的数组,所以一个变量即可解决.如果是没有排序的数组,则需要引入一个hash表来记录出现次数. 三.示例代码 class Solution { public: int RemoveDuplicatesFr

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

https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this

LeetCode 80:Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn'

leetcode || 80、Remove Duplicates from Sorted Array II

problem: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. Hide Tags Array Two Pointers 题意:对数组进行去

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

https://leetcode.com/problems/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->

LeetCode[Linked List]: 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