299. Search for an Element in a Linked List

EasyLinked ListLinked ListSearch

Given the head of a singly linked list and a target integer value, return true if the value exists in the list, otherwise return false.

Traverse the list and check each node's value against the target.

Input: Head of a singly linked list and an integer target.

Output: Boolean true if target found, false otherwise.

Examples

Example 1
Input: [1,2,3,4,5], 3
Output: true
Explanation: Traverse: 1≠3, 2≠3, 3=3 → found. Return true.
Example 2
Input: [1,2,3,4,5], 6
Output: false
Explanation: Traverse all 5 nodes — none equals 6. Return false.
Example 3
Input: [7], 7
Output: true
Explanation: Single node, value matches target. Return true.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →