Middle Node of a Linked List Problem Statement

Given the head node of a singly linked list, return a pointer to the middle node of the linked list. If there are an odd number of nodes, return the middle one. If there are an even number of nodes, return the node that is farther from the head.

Input:

The first line of input contains an integer ‘T’ representing the number of test cases. Each test case is represented by a single line containing integers that denote the nodes of a linked list, ending with -1 to signify the end of the list.

Output:

For each test case, return a pointer to the middle node of the linked list. If the list is empty, return a null pointer.

Example:

For the linked list: 1->2->3->4->null, the output will be 3 as it is the middle node.

Constraints:

  • 1 <= T <= 50
  • 1 <= N <= 4*10^4
  • -10^9 <= data <= 10^9
  • data ≠ -1
Note:
1. You do not need to print anything; just implement the function to return the result. 2. For a linked list with only one node, that node is the middle node. 3. If the list is empty, return a null pointer.
AnswerBot
4d

Return the middle node of a singly linked list, considering odd and even number of nodes.

  • Traverse the linked list with two pointers, one moving at twice the speed of the other

  • When the faster pointer r...read more

Help your peers!
Add answer anonymously...
SAP Associate Software Engineer 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