oneby7

Linked Lists Singly linked list basics

263. Search in a Linked List

Easy

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

12-7305-718

Input: head = [12, -7, 30, 5, -7, 18], key = 5

Output: true

Explanation: The nodes hold 12, -7 and 30, and then the fourth node holds 5.

Example 2

94224

Input: head = [9, 4, 22, 4], key = -4

Output: false

Explanation: Two nodes hold 4, but no node holds -4.

Example 3

Input: head = [], key = 0

Output: false

Explanation: 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.

head = [5, 11, 2, 8], key = 8

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.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.