LeetCode:203. Remove Linked List Elements

题目描述

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

Example:

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

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
start = ListNode(0)
start.next = head
tmp = head
last = start
while tmp != None:
if tmp.val == val:
last.next = tmp.next
tmp = tmp.next
else:
last = last.next
tmp = tmp.next

return start.next