(python)小菜狗算法日记(链表系列)_leetcode 83. 删除排序链表中的重复元素

给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。

示例 1:

输入: 1->1->2
输出: 1->2
示例 2:

输入: 1->1->2->3->3
输出: 1->2->3

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list

主要是[1,1,1] [1,1,1,2]这种三个的情况

# 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


 

发布了44 篇原创文章 · 获赞 0 · 访问量 1908

猜你喜欢

转载自blog.csdn.net/weixin_39331401/article/details/104558491
今日推荐