删除有序链表中重复的元素-2

# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param head ListNode类 
# @return ListNode类
#
class Solution:
    def deleteDuplicates(self , head: ListNode) -> ListNode:
        # write code here
        # 思路:对下个节点和下下个节点进行判断,如果相同则跳过所有相同的节点
        
        if not head:
            return 
        
        start = ListNode(-1)
        start.next = head
        
        # note:cur初始化为表头节点
        cur = start
        
        while cur.next and cur.next.next:
            if cur.next.val == cur.next.next.val:
                temp = cur.next.val
                while cur.next != None and cur.next.val ==temp:
                    cur.next = cur.next.next
            else:
                cur = cur.next
        
        return start.next
                
        
        
        
        
        

猜你喜欢

转载自blog.csdn.net/qq_36533552/article/details/125363911