在链表中找值为 value 的节点,如果没有的话,返回空(null)。
样例
样例 1:
输入: 1->2->3 and value = 3
输出: 最后一个结点
样例 2:
输入: 1->2->3 and value = 4
输出: null
注意事项
如果有多个相同的结点,返回找到的第一个结点
/**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/*
* @param head: the head of linked list.
* @param val: An integer.
* @return: a linked node or null.
*/
ListNode * findNode(ListNode * head, int val){
// write your code here
while(head)
{
if(head->val == val)
return head;
head = head->next;
}
return NULL;
}
};