You have been given a singly Linked List of integers. Your task is to delete the middle node of this List.
Note:
1. If there is no middle node in the list to delete, return an empty list (i.e. NULL).
2. If there are two middle nodes in the list, delete the first one.
Follow up :
Try to solve this problem in O(N) time complexity and O(1) space complexity.
Can you solve it in only one traversal of the Linked List?
Input format :
The first line of input contains an integer 'T' representing the number of test cases or queries to run. Then the test case follows.
The only line of each test case contains the elements of the singly linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.
Output format :
For each test case, print the resultant linked list after performing the required operation denoted as the elements separated by a single space and terminated by -1.
Print output of each test case in a separate line.
Note :
You are not required to print the output, it has already been taken care of. Just implement the function.
Constraints :
1 <= T <= 100
0 <= L <= 5000
1 <= data <= 10^9 and data != -1
Where 'L' is the number of nodes in the Linked List.
Time Limit: 1 sec
The task is to delete the middle node of a singly linked list of integers.
If the linked list is empty or has only one node, there is no middle node to delete.
To delete the middle node, we need to find...read more
Approach 1 :
1) Count the number of nodes in a linked list.
2) Delete n/2’th node using the simple deletion process.
TC : O(N) , N=number of nodes in the Linked List
Two traversals of the linked list is ...read more
The idea is simple and naive. We will first count the total number of nodes in the linked list (say ‘N’). Now, we know that the ceil('N' / 2)th node is our middle node of the linked list.
...read more
The idea here is to use two pointers i.e. ‘SLOW’ and ‘FAST’ and both will traverse in the linked list from beginning to end but at a different pace. The ‘SLOW’ pointer will move normally i...read more
Top Adobe Software Developer interview questions & answers
Popular interview questions of Software Developer
Reviews
Interviews
Salaries
Users/Month