LeetCode - #82 Delete Duplicate Elements in Sorted List II


insert image description here

foreword

Our community will continue to organize Gu Yi ( Netflix growth hacker, author of "iOS interview" and ACE professional fitness coach. )'s Swift algorithm solutions into text versions for everyone to learn and read.

We have updated 82 issues of the LeetCode algorithm so far, and we will keep the update time and progress ( released at 9:00 am on Monday, Wednesday, and Friday ). There is not much content in each issue, and we hope that everyone can read it on the way to work and accumulate for a long time There will be a big improvement.

If you don’t accumulate steps, you can’t reach thousands of miles; if you don’t accumulate small streams, you can’t form rivers and seas. The Swift community is with you. If you have suggestions and comments, please leave a message at the end of the article, and we will try our best to meet your needs.

Difficulty Level: Moderate

1. Description

Given the head of a sorted linked list head, remove all nodes with duplicate numbers in the original linked list, leaving only distinct numbers . Returns a sorted linked list .

2. Examples

Example 1

输入:head = [1,2,3,3,4,4,5]
输出:[1,2,5]

Example 2

输入:head = [1,1,1,2,3]
输出:[2,3]

Restrictions:

  • 链表中节点数目在范围 [0, 300]` within
  • -100 <= Node.val <= 100
  • The topic data guarantees that the linked list has been arranged in ascending order

3. Answer

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class RemoveDuplicatesfromSortedListII {
    
    
    func deleteDuplicates(_ head: ListNode?) -> ListNode? {
    
    
        if head == nil || head!.next == nil {
    
    
            return head
        }
        
        let dummy = ListNode(0)
        dummy.next = head
        var node = dummy
        
        while node.next != nil && node.next!.next != nil {
    
    
            if node.next!.val == node.next!.next!.val {
    
    
                let val = node.next!.val
                while node.next != nil && node.next!.val == val {
    
    
                    node.next = node.next!.next
                }
            } else {
    
    
                node = node.next!
            }
        }
        
        return dummy.next
    }
}
  • Main idea: iterate over the list, skipping duplicates by replacing next with next.next.
  • Time complexity: O(n)
  • Space Complexity: O(1)

Note: Swift provides "===" to compare two objects referring to the same reference

The warehouse of the algorithm solution: LeetCode-Swift

Click to go to LeetCode practice

about Us

We are jointly maintained by Swift enthusiasts. We will share technical content centered on Swift combat, SwiftUI, and Swift foundation, and also organize and collect excellent learning materials.

Guess you like

Origin blog.csdn.net/qq_36478920/article/details/131322006