Associated Course(s):
Must Do Interview Preparation

Given a singly linked list of **N** nodes. The task is to find middle of the linked list. For example, if given linked list is 1->2->3->4->5 then output should be 3.

If there are even nodes, then there would be two middle nodes, we need to print second middle element. For example, if given linked list is 1->2->3->4->5->6 then output should be 4.

**Input:**

First line of input contains number of testcases T. For each testcase, first line of input contains length of linked list and next line contains data of nodes of linked list.

**Output:**

For each testcase, there will be a single line of output containing data of middle element of linked list.

**User Task:**

The task is to complete the function **getMiddle**() which takes head reference as the only argument and should return the data at the middle node of linked list.

**Constraints:**

1 <= T <= 100

1 <= N <= 100

**Example:
Input:**

2

5

1 2 3 4 5

6

2 4 6 7 5 1

**Output:**

3

7

**Explanation:
Testcase 1:** Since, there are 5 elements, therefore 3 is the middle element at index 2 (0-based indexing).

Author: kartik

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

thanuvinu94 | 238 |

the_coder95 | 215 |

ShivayLamba | 208 |

sandeep.prajapati | 129 |

Ashish Kumar Vaishy | 114 |

the_coder95 | 1441 |

RishabhTanwar1 | 1110 |

thanuvinu94 | 914 |

tathagat289 | 668 |

themanhasnoname | 620 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4897 |

Quandray | 4547 |

Login to report an issue on this page.