当前位置: 代码迷 >> 综合 >> leetcode83. 删除排序链表中的重复元素(python)
  详细解决方案

leetcode83. 删除排序链表中的重复元素(python)

热度:77   发布时间:2023-12-24 12:44:51.0

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

示例 1:

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

示例 2:

输入: 1->1->2->3->3
输出: 1->2->3
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = Noneclass Solution:def deleteDuplicates(self, head):""":type head: ListNode:rtype: ListNode"""#第一种
#         while head is None:
#             return head#         p = head
#         current_node = head.next#         while current_node:
#             if p.val < current_node.val:
#                 p = current_node
#                 current_node = current_node.next
#             else:
#                 p.next = current_node.next #插入
#                 current_node = current_node.next
#         return head#第二种 while head is None:return headp = headwhile p and p.next:if p.val == p.next.val:p.next = p.next.nextelse:p = p.nextreturn head

 

 

  相关解决方案