LintCode Q452 Remove Linked List Elements in Python

  • Jinhai ZHOU
  • 3 Minutes
  • 2016年11月10日
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param head, a ListNode
# @param val, an integer
# @return a ListNode
# Total Runtime: 516 ms
# 100% test cases passed.
def removeElements(self, head, val):
# Write your code here
if head is None:
return None
current = head
head = ListNode(None)
head.next = current
previous = head
while current:
if current.val == val:
previous.next = current.next
current = previous
previous = current
current = current.next
return head.next
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。