Find the middle of a given linked listGiven a singly linked list, find the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the output should be 3. Show
Python program to find middle of a linked list using one traversalGiven a singly linked list, find middle of the linked list. Given a singly linked list, find middle of the linked list. For example, if given linked list is 1->2->3->4->5 then output should be 3. Method 1: Method 2:
Output:
The middle element is: 2
Method 3:
Output:
100
Article Tags :
Linked List Python Programs
Python DSA-exercises Python LinkedList-exercises Technical Scripter 2018 Practice Tags :
Linked List Middle of the Linked ListDifficulty: Easy Understanding the ProblemProblem Description Given a non-empty, singly linked list with head node head, write a program to return a middle node of the linked list. If there are even nodes, then there would be two middle nodes, we need to print the second middle element. Example 1 Input: 11->2->13->44->5 Output: 13 Explanation: The middle element of the linked list is 13.Example 2 Input: 10->2->34->24->15->60 Output: 24 Explanation: As there are even number of nodes, return 24 as it is the second node among two middle elements.Solutions
Before moving forward with the question, you must understand the properties of a linked list. You can try this problem here. 1. Two-PassThe question demands to find the middle of a singly linked list. We can simply find the total length of the linked list, in this way we can identify which node falls in the middle. To find the middle node, we can traverse again until we reach (length/2)th node. Solution Steps
Pseudo Code Complexity Analysis Time Complexity: O(n) Space Complexity: O(1) Critical Ideas To Think
2. One-PassAnother way to solve this problem is to use a little trick. Instead of traversing twice, we can create two-pointers say slow_ptr and fast_ptr.We can make the fast_ptr twice as fast as slow_ptr. So, When the fast_ptr will reach to the end of the linked list, slow_ptr would still be at the middle, thereby pointing to the mid of the linked list. Solutions Steps
Pseudo Code int middleNode(ListNode head) { ListNode slow = head ListNode fast = head while (fast != null and fast.next != null) { slow = slow.next fast = fast.next.next } return slow.data }Complexity Analysis Time Complexity: O(n) Space Complexity: O(1) Critical Ideas To Think
Comparison Of Different ApproachesSuggested Problems To Solve
Happy coding! Enjoy Algorithms. How do you find the middle element in a single traversal in a singly linked list?Given a singly linked list, find the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the output should be 3. If there are even nodes, then there would be two middle nodes, we need to print the second middle element. How do you find the middle element of a linked list in one pass in C++?In order to find middle element of linked list in one pass, you need to maintain two pointers, one increment at each node while other increments after two nodes at a time. By having this arrangement, when first pointer reaches end, second pointer will point to middle element of linked list. What is the time complexity for finding the middle node of a linked list?The running time of finding the middle element this way with two pointers is O(n) because once we pass through the entire linked list of n elements, the slower pointer is at the middle node already. How to Find Middle Element of a Linked List?Method 1: Finding middle element of a linked in two traversal i) Traverse a linked list and find the length of a linked list. By using this method we can solve this problem in two traversal. Java
Can we do better than that? How do you find the middle element in a singly linked list in one pass? Yes we can solve this problem in a single traversal. Let’s discuss our second approach. Check if a linked list is palindrome or not Sort a linked list of 0s, 1s and 2s Method 2: Find middle element in linked list in one pass using two pointers. i) Use two pointer slow and fast. Initialized both the pointers to the head of a linked list. Programming questions on linked list Find Middle Element of a Linked List C++ Code/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* middleNode(ListNode* head) {
int n = 0;
ListNode* temp = head;
while(temp) {
n++;
temp = temp->next;
}
temp = head;
for(int i = 0; i < n / 2; i++) {
temp = temp->next;
}
return temp;
}
}; Solution 2: [Efficient] Tortoise-Hare-Approach Unlike the above approach, we don’t have to maintain nodes count here and we will be able to find the middle node in a single traversal so this approach is more efficient. Intuition: In the Tortoise-Hare approach, we increment slow ptr by 1 and fast ptr by 2, so if take a close look fast ptr will travel double than that of the slow pointer. So when the fast ptr will be at the end of Linked List, slow ptr would have covered half of Linked List till then. So slow ptr will be pointing towards the middle of Linked List. Approach:
Code: C++ Code/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* middleNode(ListNode* head) {
ListNode *slow = head, *fast = head;
while (fast && fast->next)
slow = slow->next, fast = fast->next->next;
return slow;
}
};
|