Thursday, February 4, 2016

[LintCode] 225 Find Node in Linked List

Description
Find a node with given value in a linked list. Return null if not exists.


Example
Given 1->2->3 and value = 3, return the last node.
Given 1->2->3 and value = 4, return null.


Code
/**
 * Definition for ListNode
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param head: the head of linked list.
     * @param val: an integer
     * @return: a linked node or null
     */
    public ListNode findNode(ListNode head, int val) { 
        // Write your code here
        while (head != null) {
            if (head.val == val) {
                break;
            }
            else {
                head = head.next;
            }
        }
        return head;
    }  
}