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->3.

【题意】

给定一个有序链表,删出其中重复出现的值。 注意,Remove Duplicates from Sorted List使链表中的值唯一化,而本题是直接把有重复的值剔除。

【思路】

仍然维护2个指针prev, cur。

prev充当新链表的尾结点指针

cur用来扫描链表

【代码】

/**
 * 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) {
        if(head==NULL)return head;
        if(head->next==NULL)return head;

		ListNode*newHead=NULL;
		ListNode*prev=NULL;
		ListNode*cur=head;
		while(cur){
			if(cur->next!=NULL){
				//判断当前结点值是否重复
				if(cur->val == cur->next->val){
					//如果判断是重复数字,则跳过与当前结点值相等的结点
					int val = cur->val;
					while(cur && cur->val==val)cur=cur->next;
				}
				else{
					//如果不重复,则接到新链表上
					if(prev==NULL) newHead=cur;
					else prev->next=cur;
					prev=cur;
					cur=cur->next;
					prev->next=NULL;    //prev这里充当的是链表尾结点的角色,因此next指针需要置NULL
				}
			}
			else{
				//如果已经是最后一个结点,之间连到新链表上
				if(prev==NULL)newHead=cur;
				else prev->next=cur;
				cur=cur->next;
			}
		}

        return newHead;
    }
};

LeetCode: Remove Duplicates from Sorted List II [083],布布扣,bubuko.com

时间: 2024-10-11 06:29:22

LeetCode: Remove Duplicates from Sorted List II [083]的相关文章

[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 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

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 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 Array II (删除重复元素II)

Remove Duplicates from Sorted Array II Leetcode 题目: 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

Leetcode: 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 numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->

[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 (C++)

题目: 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]. Tag: Array; Two Pointers 体会: 继续是quicksort中par