![](https://img.haomeiwen.com/i14924385/ad7c802b975e6158.jpg)
LeetCode 203. Remove Linked List Elements
Description
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
描述
删除链表中等于给定值 val 的所有节点。
示例:
输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
实现
- 本题目考察基本的链表操作.
# -*- coding: utf-8 -*-
# @Author: 何睿
# @Create Date: 2019-01-20 21:12:46
# @Last Modified by: 何睿
# @Last Modified time: 2019-01-20 21:23:10
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
# 添加一个辅助节点
node = ListNode(0)
node.next = head
res = node
while node and node.next:
if node.next.val == val:
temp = node.next
node.next = temp.next
del temp
else:
node = node.next
return res.next
网友评论