题目描述
删除链表中等于给定值 val 的所有节点。
示例:
输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
我的解法——pre指针(AC)
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
pre = head
while(pre):
if(pre.val == val):
pre = pre.next
else:
break
head = pre
if(not head):
return head
now = pre.next
while(now):
if(now.val == val):
now = now.next
pre.next = now
else:
pre = pre.next
now = now.next
return head