Remove Linked List Elements
前后夹心指针,cur指针是要删除的对象则pre指针指向cur.next然后保持原地不动,不是则按常规往前跳
class Solution(object):
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
dummy = ListNode('a')
dummy.next = head
pre, cur = dummy, head
while cur:
if cur.val == val:
pre.next = cur.next
cur = cur.next
continue
pre = cur
cur = cur.next
return dummy.next