美文网首页
82. Remove Duplicates from Sorte

82. Remove Duplicates from Sorte

作者: April63 | 来源:发表于2018-06-19 11:54 被阅读0次

这一题好像是剑指offer上的,需要注意用pre指针
代码如下:

Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head or not head.next:
            return head
        dumpy = ListNode(0)
        dumpy.next = head
        pre = dumpy
        curr = head 
        p = head.next
        while p:
            while p and p.val == curr.val:
                p = p.next
            if curr.next != p:
                pre.next = p
                curr = p
                if curr:
                    p = curr.next
            else:
                pre = curr
                curr = curr.next
                if curr:
                    p = curr.next
        return dumpy.next

相关文章

网友评论

      本文标题:82. Remove Duplicates from Sorte

      本文链接:https://www.haomeiwen.com/subject/ugraeftx.html