Skip to content

Latest commit

 

History

History
31 lines (31 loc) · 697 Bytes

File metadata and controls

31 lines (31 loc) · 697 Bytes

Middle of the Linked List


  • Question:

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

If there are two middle nodes, return the second middle node.


  • Example:

ALT

Input: head = [1,2,3,4,5]

Output: [3,4,5]

Explanation: The middle node of the list is node 3.


  • Solution:

Code :

class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode p1,p2;
        p1=head;
        p2=head;
        while(p2!=null && p2.next!=null)
        {
            p1=p1.next;
            p2=p2.next.next;
        }
        return p1;
    }
}