Delete the Middle Node from a Singly Linked List

Given a singly linked list of integers, the task is to remove the middle node from this list.

Input:

The first line of input includes an integer 'T' which denotes the number of test cases. 
Each test case consists of a single line containing the linked list elements separated by spaces, ending with -1 (which is not part of the list).

Output:

For every test case, produce the updated linked list after removing the middle node, separated by spaces and terminating with -1.
Render the output for each test case on a distinct line.

Example:

Input:
2
1 3 4 7 1 2 6 -1
1 2 3 4 -1
Output:
1 3 4 1 2 6 -1
1 2 4 -1

Constraints:

  • 1 <= T <= 100
  • 0 <= L <= 5000
  • 1 <= data <= 109 and data ≠ -1

Notes:

1. Return an empty list (i.e., NULL) if no middle node to delete exists.
2. If there are two middle nodes, remove the first one.

Follow-up: Can you achieve the solution in O(N) time and O(1) space complexity with just a single traversal of the list?

Be the first one to answer
Add answer anonymously...
GoMechanic Software Developer Intern 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