Remove all elements from a linked list of integers that have value val.

Example:

Input: 1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5

Solution in python:

# 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:
        begin = ListNode(0)
        prior = begin
        begin.next = head
        while head != None:
            if head.val == val:
                prior.next = head.next
            else:
                prior = head
            head = head.next
        return begin.next
最后修改日期: 2021年1月19日

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。