Finding middle element in a linked list| GeeksForGeeks| Problem of the Day | Java| Easy Solution

preview_player
Показать описание
In this question I have solved gfg problem of the day using very simple and easy to understand approach.

-----------------------------------------------------------------------------------------------------------------------
Finding middle element in a linked list

Given a singly linked list of N nodes.
The task is to find the middle of the linked list. For example, if the linked list is
1-2-3-4-5, then the middle node of the list is 3.
If there are two middle nodes(in case, when N is even), print the second middle element.
For example, if the linked list given is 1--2--3--4--5--6, then the middle node of the list is 4.

Example 1:

Input:
LinkedList: 1--2--3--4--5
Output: 3
Explanation:
Middle of linked list is 3.
Example 2:

Input:
LinkedList: 2--4--6--7--5--1
Output: 7
Explanation:
Middle of linked list is 7.
-------------------------------------------------------------------------------------------------------------------

***************************************************************************

#dsa #programming #gfg #gfgpotd #problemsolving #coding #softwareengineer #faang #competitiveprogramming #dsasheet #interviewpreparation #coding #gfg #geeksforgeeks #leetcode #potd #coding #india #easyexplaination #leetcode #leetcodechallenge #leetcodedailychallenge
#java #codinginterviews #problemoftheday #algorithm #datastructures #hindi #bitmanipulation #bits #competitiveprogramming
Рекомендации по теме
Комментарии
Автор

Solution:
class Solution
{
int getMiddle(Node head)
{
Node s=head;
Node f=head;
while(f!=null && f.next!=null){
s=s.next;
f=f.next.next;
}
return s.data;
}
}

Codey_