LeetCode82 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.(Medium)

For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

分析:

链表去重的follow-up,但是题号这个在前面,要把有重复元素,则所有该元素均删掉。

注意:

1. 使用dummy node处理头结点被删除掉的情况。

2. 只要有head-> next -> val存在时,先判断head -> next是否为空。

代码:

 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         ListNode dummy(0);
13         dummy.next = head;
14         head = &dummy;
15         while(head -> next != nullptr && head -> next -> next != nullptr) {
16             if (head -> next -> val == head -> next -> next -> val) {
17                 int val = head -> next -> val;
18                 while (head -> next != nullptr && head -> next -> val == val) { //只要取head->next,先判断
19                     ListNode* temp = head -> next;
20                     head -> next = head -> next -> next;
21                     delete temp;
22                 }
23             }
24             else {
25                 head = head -> next;
26             }
27         }
28         return dummy.next;
29     }
30 };
时间: 2024-08-01 22:41:53

LeetCode82 Remove Duplicates from Sorted List II的相关文章

LeetCode(82): 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]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

LintCode - Remove Duplicates from Sorted List II Show result

LintCode - Remove Duplicates from Sorted List II Show result LintCode - Remove Duplicates from Sorted List II Show result Web Link Description Code - C Tips Web Link http://www.lintcode.com/en/problem/remove-duplicates-from-sorted-list-ii/ Descriptio

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

[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

本文为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

80. Remove Duplicates from Sorted Array II

/* * 80. Remove Duplicates from Sorted Array II * 2016-5-13 by Mingyang * 这里用了通式,就是k的值可以根据需要随时改变,注意这里++j的用法,最后return ++j * 注意的是题目不光要返回int,还要把array给换了 */ public int removeDuplicates2(int[] A,int k){ int len=A.length; if(len<k) return len; int j=0; int

[LeetCode][JavaScript]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 (删除重复元素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