LeetCode刷题_83. Remove Duplicates from Sorted List

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pku_langzi/article/details/84939156

地址:https://leetcode.com/problems/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

Example 2:

Input: 1->1->2->3->3
Output: 1->2->3

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        previous = head
        current = head
        while current and current.next:
            while current and current.val == previous.val:
                current = current.next
            if current:
                previous.next = current
                previous = current
            else:
                previous.next = None
        return head
     

猜你喜欢

转载自blog.csdn.net/pku_langzi/article/details/84939156