
正文
LeetCode--203--删除链表中的节点
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
问题描述:
删除链表中等于给定值 val 的所有节点。
示例:
输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
方法1:防止[1,1,1,1] 1 用while head。
class Solution(object):
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
if head == None:
return
while head and head.val == val:
head = head.next
p = head
while p:
if p.next and p.next.val == val:
p.next = p.next.next else:
p = p.next
return head
2018-09-17 19:34:21








