leetcode之Remove Linked List Elements(203)

题目:

删除链表中等于给定值 val 的所有节点。

示例:

输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5

python代码:

class Solution(object):
    def removeElements(self, head, val):
        if head == None:
            return None
        head.next = self.removeElements(head.next, val)
        return head if head.val != val else head.next

心得:递归代码量少,容易理解。

猜你喜欢

转载自blog.csdn.net/cuicheng01/article/details/81388906
今日推荐