【leetcode❤python】 203. Remove Linked List Elements

#-*- coding: UTF-8 -*-
# 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
        """
        if head==None:return []
        dummy=ListNode(-1)
        dummy.next=head
        p=dummy
        
        while head:
            
            if head.val==val:
                p.next=head.next
                head=p
            
            p=head
            head=head.next
            
            
        return dummy.next
       

posted @ 2016-11-13 19:20  火金队长  阅读(267)  评论(0编辑  收藏  举报