在链表中找值为 value 的节点,如果没有的话,返回空。
样例
给出 1->2->3
和 value = 3, 返回最后一个节点 last node.
给出 1->2->3
和 value = 4, 返回空。
实现代码:
"""
Definition of ListNode
class ListNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param: head: the head of linked list.
@param: val: An integer.
@return: a linked node or null.
"""
def findNode(self, head, val):
# write your code here
while head:
if head.val == val:
return head
else:
head = head.next
return head