1. 程式人生 > >LeetCode(83)Remove Duplicates from Sorted List

LeetCode(83)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->2->3.

題目分析:

本題是給定一個連結串列,對連結串列進行濾重,使得每個數只出現一次。
之前的一道題目 LeetCode(26)Remove Duplicates From Sorted Array 非常相似。之前這道題目是給定一個數組,對陣列進行濾重,使得每個數只出現一次。
有了之前這倒題目的鋪墊,使用相同的思路完成即可。核心思路是,理解為不斷地去生成最終結果,即不斷地把只出現過一次的元素加入到現有的結果中去更新最終結果。如果理解為在原來的基礎上進行刪除,就會做得比較麻煩了。特別是LeetCode(26)的資料結構為陣列,每次刪除都要把右側陣列左移,非常花時間。刪除連結串列元素的時間開銷雖然小於刪除陣列元素的時間開銷,但是使用生成而非刪除這樣的思路還是不錯的。

程式碼如下:

//84ms過大集合
class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        if(head==NULL||head->next==NULL)
            return head;
        ListNode* p=head;
        ListNode* q=head->next;
        while(q!=NULL){
            if(q->val==p->val) {
                q=q->next;
            }else{
                p->next=q;
                p=q;
                q=q->next;
            }
        }
        p->next=NULL;
        return head;
    }
};

update 1:  2014 - 10- 07
用生成新陣列的思路,重構

class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        if(head==NULL)
            return head;
        ListNode* p=head;
        ListNode* q=head->next;
        while(q!=NULL){
            if(q->val==p->val) {
                p->next = q->next;
            }else{
                p=q;
            }
            q = q->next;
        }
        return head;
    }
};

另外一種寫法基本一樣, 只是變數名更清楚,是這樣的:

class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        if (head == NULL) return head;
        ListNode* p_current = head;
        ListNode* p_next = head->next;
       
        while (p_current != NULL && p_next != NULL) {
            if (p_current->val == p_next-> val) {
                p_current->next = p_next->next;
            } else {
                p_current = p_current->next;
            }
            p_next = p_next->next;
       }
       return head;
    }
};



相關推薦

leetCode 83.Remove Duplicates from Sorted List(刪除排序鏈表的反復) 解題思路和方法

排序 back ace 去除 adding 思路 詳細 init ica Given a sorted linked list, delete all duplicates such that each element appear only once.

[leetcode] 83. Remove Duplicates from Sorted List 解題報告

ted leetcode index turn pre div logs 備份 cat Given a sorted linked list, delete all duplicates such that each element appear only once. Fo

leetcode-83-Remove Duplicates from Sorted List

節點 sort listnode 內存 sorted val 內存空間 spa appear 題目描述: Given a sorted linked list, delete all duplicates such that each element appear only

[leetcode]83. Remove Duplicates from Sorted List有序鏈表去重

nbsp IV element pan while mage next ima ret Given a sorted linked list, delete all duplicates such that each element appear only once. Ex

(Java) LeetCode 83. Remove Duplicates from Sorted List —— 刪除排序鏈表中的重復元素

def TP htm val brush etc 過程 表頭 遞歸解法 Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1:

LeetCode 83. Remove Duplicates from Sorted List

list null each ron mov lee ive div ack 分析 難度 易 來源 https://leetcode.com/problems/remove-duplicates-from-sorted-list/ 題目 Given a sorted lin

leetcode 83. Remove Duplicates from Sorted List (easy)

Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->2 Output: 1->2

#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

[leetcode]83. Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->2 Output: 1->2

[LeetCode] 83. Remove Duplicates from Sorted List

題目 Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->2 Outp

leetcode 83 Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->2 Output: 1->2

LeetCode 83. Remove Duplicates from Sorted List(java)

You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nu

LeetCode--83. Remove Duplicates from Sorted List & 82. Remove Duplicates from Sorted List II

題目連結:https://leetcode.com/problems/remove-duplicates-from-sorted-list/ https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 題目一:要求

【python3】leetcode 83. Remove Duplicates from Sorted List (easy)

83. Remove Duplicates from Sorted List (easy) Given a sorted linked list, delete all duplicates such that each element appear only once.

leetCode 83.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-&

LeetCode(83)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

[LeetCode]83. Remove Duplicates from Sorted List(刪除有序連結串列的重複元素 )

83. Remove Duplicates from Sorted List 原題連結 Given a sorted linked list, delete all duplicates such

LeetCode Linked List Easy 83. Remove Duplicates from Sorted List

pro example 給定 https image ica com 描述 問題 Description Given a sorted linked list, delete all duplicates such that each element appear on

[LeetCode][Java] Remove Duplicates from Sorted List II

gin -m || 代碼 number 算法分析 add dup adding 題意: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only

83. Remove Duplicates from Sorted List【easy】

pen dup pub writing bsp scu call sort cnblogs 83. Remove Duplicates from Sorted List【easy】 Given a sorted linked list, delete all duplic