Skip to main content

Midpoint of Linked List

/*

    Following is the Node class already written for the Linked List

    class Node<T> {
        T data;
        Node<T> next;
    
        public Node(T data) {
            this.data = data;
        }
    }

*/

public class Solution {
    
    public static Node<Integer> midPoint(Node<Integer> head) {
        //Your code goes here

        if(head == null){
            return null;
        }

        Node<Integer> slow = head;
        Node<Integer> fast = head;

        while(fast.next != null && fast.next.next != null){
            fast = fast.next.next;
            slow = slow.next;
        } return slow;


    }

}


jab tak fast.next null nhi hai tab tak chlte rhna hai ya fast ke next ka next null nhi
hai chalte rhna hai agr inme se koi ek v false ho jata hai to ruk jana hai

yaani agar null ho jata hai to condition false ho jayegi

Comments