leetcode Remove Linked List Elements

https://leetcode.com/problems/remove-linked-list-elements/

注意使用最常用的删除linkedlist node的方法,不要用那种trick的办法。


my code:

class Solution(object):
    def removeElements(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """
        if head == None:
            return []
            

       
        i = head
        j = head
        while i:
            #print (i.val, i.next.val)
            if i.val == val:
                j.next = i.next
                
            else:
                j = i
            i = i.next
        if head.val == val:
            head = head.next
        return head

参考:

里面还提供了加一个dummy node 的方法

http://bookshadow.com/weblog/2015/04/24/leetcode-remove-linked-list-elements/

你可能感兴趣的:(leetcode Remove Linked List Elements)