(Python) algorithm dog dishes diary (list series) _leetcode 83. remove sorts the list of repeating elements

Given a sorted list, delete all the duplicate elements, so that each element occurs only once.

Example 1:

Input: 1-> 1-> 2
Output: 1-> 2
Example 2:

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

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/remove-duplicates-from-sorted-list

Mainly [1,1,1] [1,1,1,2] this case three

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

class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        cur = head
        while(head and head.next):
            if head.val == head.next.val:
                head.next = head.next.next
            # if head.next:
            #      if head.val != head.next.val:
            #          head = head.next
            # else:
            #     head = head.next
            else:
                head = head.next
        return cur


 

Published 44 original articles · won praise 0 · Views 1908

Guess you like

Origin blog.csdn.net/weixin_39331401/article/details/104558491