Find a Node in Linked List

Given a singly linked list of integers, your task is to implement a function that returns the index/position of an integer value 'N' if it exists in the linked list. Return -1 if the value is not found.

Input:

The first line contains an integer 'T' representing the number of test cases. 
For each test case, the first line contains the elements of the singly linked list separated by spaces.
The second line of each test case contains an integer 'N', which is the value to be searched in the list.

Output:

For each test case, return the index/position of 'N' in the singly linked list. Print -1 if it is not found. The result of each test case should appear on a new line.

Example:

If the linked list is [10, 20, 30, 40] and 'N' = 30, the function should return 2 since 30 is located at index 2.

Constraints:

  • 1 <= T <= 102
  • 0 <= M <= 105, where 'M' is the length of the singly linked list.
Note:
Indexing of the list starts from 0.
The list terminates at -1 which is not a list element.
AnswerBot
4mo

Implement a function to find the index of a given integer in a singly linked list.

  • Traverse the linked list while keeping track of the index of each element.

  • Compare each element with the target integer...read more

Help your peers!
Select
Add answer anonymously...

Hewlett Packard Enterprise Full Stack Developer interview questions & answers

A Full Stack Developer was asked Q. Merge Sort Problem Statement You are given a sequence of numbers, ARR. Your task...read more
A Full Stack Developer was asked Q. Count Set Bits Problem Statement Given a positive integer N, compute the total n...read more
A Full Stack Developer was asked Q. Smallest Number with Given Digit Product Given a positive integer 'N', find and ...read more

Popular interview questions of Full Stack Developer

A Full Stack Developer was asked Q1. Merge Sort Problem Statement You are given a sequence of numbers, ARR. Your task...read more
A Full Stack Developer was asked Q2. Count Set Bits Problem Statement Given a positive integer N, compute the total n...read more
A Full Stack Developer was asked Q3. Smallest Number with Given Digit Product Given a positive integer 'N', find and ...read more
Hewlett Packard Enterprise Full Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits