Description: Remove all elements from a linked list of integers that have value val.
Link: https://leetcode.com/problems/remove-linked-list-elements/
Example:
Input: 1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5
思路: 删除单链表中元素值为val 的节点,遍历一遍,遇到就删除。
class Solution(object): def removeElements(self, head, val): """ :type head: ListNode :type val: int :rtype: ListNode """ if not head: return None rh = ListNode(0) rh.next = head pre = rh p = head while p: if p.val == val: pre.next = p.next else: pre = p p = p.next return rh.next
日期: 2020--12-1 时间过慢一点呀