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

题意:将已排序的链表中所有重复的元素移除

思路:

三个个指针,pre, cur, next

一个布尔变量is_appear,表示cur指针指向的值之前是否已经出现过

检查cur, next指向的节点的值是否相同,

相同的话,删除cur

不同的话,如果is_appear为true,删除cur,否则不删除

删除的话,只移动cur, next指针

不删除的话,要移动pre, cur, next三个指针

复杂度:时间O(n),空间O(1)

/**
 * 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 || !head->next) return head;
    	ListNode tem(0);
    	ListNode *dummy = &tem;
    	dummy->next = head;
    	ListNode *pre = dummy, *cur = head, *next = head->next;
    	bool is_appear = false;
    	while(cur && next){
    		if(cur->val == next->val || is_appear){
    			is_appear = cur->val == next->val;
    			pre->next = next;
    			delete cur;
    			cur = next;
    			next =next->next;
    		}else{
    			pre = cur;
    			cur = next;
    			next = next->next;
    			is_appear = false;
    		}
    	}
    	if(is_appear){
    		pre->next = NULL;
    		delete cur;
    	}
    	return dummy->next;
    }
};

Leetcode 线性表 Remove Duplicates from Sorted List II

时间: 2024-10-14 14:29:12

Leetcode 线性表 Remove Duplicates from Sorted List II的相关文章

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 Array

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array Total Accepted: 14789 Total Submissions: 46601 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new l

Leetcode 线性表 Remove Duplicates from Sorted List

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted List Total Accepted: 14961 Total Submissions: 43772 Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->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]. 说明: 1)设个标志可实现 实现: 1 class Solution { 2 public

leetcode:82. Remove Duplicates from Sorted List II(Java)解答

转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50411033 题目地址:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have

leetcode笔记:Remove Duplicates from Sorted Array II

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

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】#82. Remove Duplicates from Sorted List II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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-&