【LeetCode】203.移除链表元素

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time: 2019/3/26
# @Author: xfLi
# The file...

def removeElements(head, val):
    if not head:
        return head
    # 因为有可能头结点会被删除,所以将头结点放到最后最后处理
    cur = head
    while cur:
        if cur.next and cur.next.val == val:
            cur.next = cur.next.next
        else:
            cur = cur.next
    if head.val == val:  #最后处理头结点的的情况
        head = head.next
    return head

猜你喜欢

转载自blog.csdn.net/qq_30159015/article/details/88831549