Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Middle Of Linked List
EASY
20 mins
129 upvotes
Linked List
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Linked List
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Middle Of Linked List

Contributed by
Ashwani
Easy
yellow-spark
0/40
Avg time to solve 20 mins
Success Rate 80 %
Share
129 upvotes

Problem Statement

Given the head node of the singly linked list, return a pointer pointing to the middle of the linked list.

If there are an odd number of elements, return the middle element if there are even elements return the one which is farther from the head node.

For example, let the linked list be 1->2->3->4->null

add-image

Since the number of elements in this linked list is 4 so we have 2 middle elements, i.e. 2 and 3, but we return 3 as it is farther from the head node, i.e. 1.

Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1 :
2
1 2 3 4 5 -1
4 0 32 5 48 6 -1
Sample Output 1 :
3
5
Explanation Of Sample Input 1 :
For the first test case:

The linked List is 1->2->3->4->5->NULL
We can clearly see that there are 5 elements in the linked list and the middle element is 3 hence we return a pointer poiniting to the middle element i.e 3.

For the second test case:

The linked List is : 4->0->32->5->48->6->NULL

We have 6 elements in the linked list. we will have 2 middle elements i.e 32 and 5. Since it is specified to return the element farther from the root node, we return 5.
Sample Input 2 :
2
2 69 7 -1
1 4 5 8 9 6 3 -1
Sample Output 2 :
69
8
Reset Code
Full screen
Auto
copy-code
Console