AfterAcademy Tech
•
10 Aug 2020

Difficulty: Easy
Problem 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.
Before moving forward with the question, you must understand the properties of a linked list.
You can try this problem here.
The 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
p, pointing to the head.p reaches to the end of the linked list, thereby find the length of the list.p to head again. Now, increment p length/2 times. Now, the p is at the middle of the linked list node. Return the value at pPseudo Code
int middleNode(ListNode head) {
int l=0
ListNode p = head
while (p != null) {
p = p.next
l = l + 1
}
p = head
int c = 0
while (c < l/2) {
p = p.next
c = c + 1
}
return p.data
}
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Critical Ideas To Think
p with the head?Another 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
slow_ptr and fast_ptr pointing to the head initially.fast_ptr by 2 and slow_ptr by 1 positions until fast_ptr and fast_ptr.next is not NULLslow_ptr.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
fast != null and fast.next != null ?
Happy coding!
Enjoy Algorithms.
AfterAcademy Tech
In this blog, we will discuss the types of linked list and basic operations that can be performed on a linked list.

AfterAcademy Tech
The problem is about reversing a Linked List. We are given the head node of the Linked List and we have to return the reversed Linked List's head. This is an interview problem asked in companies like Microsoft, Amazon and Adobe.

AfterAcademy Tech
Array and Linked List are the two most used data structures. It's really important to understand and compare the advantages and disadvantages of both array and linked list. In this blog, we will compare these two linear data structures. So let's get started.

AfterAcademy Tech
Given a binary tree, flatten it to a linked list in-place. After flattening, the left of each node should point to NULL and right should contain the next node in level order so that it resembles a singly linked list.
