83. 删除排序链表中的重复元素
作者:互联网
83. 删除排序链表中的重复元素 # 由于是已排序的链表,判断前后是否为相同元素如果是则连接下下个不是则向前移动 #code: # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: if not head: return head cur = head while cur.next: if cur.val==cur.next.val: cur.next=cur.next.next else: cur = cur.next return head #虽然本题很简单,但可以用多种方法去试试 #现在使用递归的方法 class Solution(object): def deleteDuplicates(self, head): if not head or not head.next: return head if head.val!=head.next.val: head.next =self.deleteDuplicates(head.next) else: move = head.next while move.next and head.val==move.next.val: move = move.next return self.deleteDuplicates(move) return head
标签:head,cur,val,self,move,next,链表,83,排序 来源: https://www.cnblogs.com/blogy-y/p/16639312.html