美文网首页
203. 移除链表元素

203. 移除链表元素

作者: 好吃红薯 | 来源:发表于2019-05-20 16:17 被阅读0次

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

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

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

class Solution:
    def removeElements(self, head: ListNode, val: int) -> ListNode:
        empty_head = ListNode(-1)
        empty_head.next = head
        cur = empty_head
        while cur.next:
            if cur.next.val == val:
                cur.next = cur.next.next
            else:
                cur = cur.next
        return empty_head.next

相关文章

网友评论

      本文标题:203. 移除链表元素

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