删除有序链表的节点

# 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: ListNode) -> ListNode:
        # 因为有序,不用缓冲区
        if not head:
            return None
        pre = head
        current = head.next
        while current:

            if pre.val == current.val:
                pre.next = current.next
            else:
                pre = current
            current = current.next

        return head

posted @ 2021-03-07 21:36  KbMan  阅读(63)  评论(0编辑  收藏  举报