码迷,mamicode.com
首页 > 其他好文 > 详细

[LC] 203. Remove Linked List Elements

时间:2019-09-25 12:23:38      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:elements   head   element   lin   from   style   __init__   ini   not   

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

# 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:
        dummy = ListNode(-1)
        dummy.next = head
        cur = dummy
        while cur.next is not None:
          if cur.next.val != val:
            cur = cur.next
          else:
            cur.next = cur.next.next
        return dummy.next

 

[LC] 203. Remove Linked List Elements

标签:elements   head   element   lin   from   style   __init__   ini   not   

原文地址:https://www.cnblogs.com/xuanlu/p/11583638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!