Linked Lists Singly linked list basics
263. Search in a Linked List
You are given head, the first node of a singly linked list, and an integer key. Return true if some node
of the list holds the value key, and false if no node does.
An empty list has no nodes, and its head is null. The examples write a list as its values from the head on, so
head = [4,1,6] is the list 4 → 1 → 6. Your function gets only the head node, not this array.
Example 1
Input: head = [12, -7, 30, 5, -7, 18], key = 5
trueExplanation: The nodes hold 12, -7 and 30, and then the fourth node holds 5.
Example 2
Input: head = [9, 4, 22, 4], key = -4
falseExplanation: Two nodes hold 4, but no node holds -4.
Example 3
Input: head = [], key = 0
falseExplanation: The list is empty, so no node holds any value.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
0 ≤ number of nodes ≤ 105-109 ≤ Node.val ≤ 109-109 ≤ key ≤ 109
Clarifying questions
What you would ask an interviewer before writing any code.