[Leetcode] Remove duplicate 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,
Given1->2->3->3->4->4->5, return1->2->5.
Given1->1->1->2->3, return2->3.

这题和Remove duplicate from sorted list的区别在于,本题中,只要结点只要出现重复则该值相等的结点都要删除,上题中留一个不删。

思路:这里有可能有修改表头(如:1->1->-1>2>3),一般修改表头的题目都会需要一个辅助指针,所以要新建一个结点。遍历链表,遇到相等的相邻结点,直接继续遍历;遇到不相等的两相邻结点时,若pre->next=cur说明cur没有重复的,pre=pre->next即可,若是不等于说明,可能有重复,则,pre连接cur但是pre不移动,需重新进入循环检验是否没有重复(没有重复时,pre->next=cur),直到没有重复结点。源代码

 1 class Solution {
 2 public:
 3     ListNode *deleteDuplicates(ListNode *head)
 4     {
 5         if(head==NULL)  return head;
 6
 7         ListNode *newList=new ListNode(-1);
 8         newList->next=head;
 9         ListNode *pre=newList;
10         ListNode *cur=head;
11
12         while(cur)
13         {
14             while(cur->next&&pre->next->val==cur->next->val)
15             {
16                 cur=cur->next;
17             }
18
19             if(pre->next==cur)
20                 pre=pre->next;
21             else
22             {
23                 pre->next=cur->next;
24             }
25
26             cur=cur->next;
27         }
28         return newList->next;
29     }
30 };
时间: 2024-10-08 08:03:29

[Leetcode] Remove duplicate from sorted list ii 从已排序的链表中删除重复结点的相关文章

[Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

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]. 题意:可以保留一个重复的元素. 思路:第一种是和Remove duplicates from sorte

leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)

题目: 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]. 说明: 1)设个标志可实现 实现: 1 class Solution { 2 public

[Leetcode] 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 in place with constant memory. For example,Given input array A =[1

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

[leetcode]Remove Duplicates from Sorted List II @ Python

原题地址:https://oj.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-&g

LeetCode: Remove Duplicates from Sorted List II [083]

[题目] 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-

leetcode——Remove Duplicates from Sorted List II 删除排序字符串中重复字符(AC)

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

[LeetCode] Remove Duplicates from Sorted Array II [27]

题目 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]. 原题链接(点我) 解题思路 移除数组中重复次数超过2次以上出现的数,但是可以允许重复2次

LeetCode: Remove Duplicates from Sorted Array II [080]

[题目] 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]. [题意] 给定一个有序数组,给数组去重,和Remove Duplicates fro