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.
Be the first one to answer
Add answer anonymously...
OodlesTechnologies Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter