题目描述:

leetcood学习笔记-203-移除链表元素

方法:#在改pre链表时 head中的值也改变

class Solution(object):
    def removeElements(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """
        pre = ListNode(0)
        pre.next = head
        while pre.next!=None:
            if pre.next.val == val:
                if pre.next == head:
                    head = head.next
                    pre.next = pre.next.next
                else:
                    pre.next = pre.next.next
            else:
                pre = pre.next
        return head

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-12
  • 2022-12-23
  • 2022-12-23
  • 2021-09-15
猜你喜欢
  • 2021-07-15
  • 2021-05-22
  • 2022-03-05
  • 2022-02-26
  • 2021-08-11
相关资源
相似解决方案