Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Java Program For Removing Middle Points From a Linked List Of Line Segments

  • Last Updated : 27 Dec, 2021

Given a linked list of coordinates where adjacent points either form a vertical line or a horizontal line. Delete points from the linked list which are in the middle of a horizontal or vertical line.

Input:   (0,10)->(1,10)->(5,10)->(7,10)
Output: Linked List should be changed to following
The given linked list represents a horizontal line from (0,10) 
to (7, 10) followed by a vertical line from (7, 10) to (7, 5), 
followed by a horizontal line from (7, 5) to (40, 5).

Input: (2,3)->(4,3)->(6,3)->(10,3)->(12,3)
Output: Linked List should be changed to following
There is only one vertical line, so all middle points are removed.

Source: Microsoft Interview Experience

The idea is to keep track of the current node, next node, and next-next node. While the next node is the same as the next-next node, keep deleting the next node. In this complete procedure, we need to keep an eye on the shifting of pointers and checking for NULL values.
Following are implementations of the above idea. 


// Java program to remove middle points in 
// a linked list of line segments,
class LinkedList
    // Head of list
    Node head;  
    // Linked list Node
    class Node
        int x,y;
        Node next;
        Node(int x, int y)
            this.x = x;
            this.y = y;
            next = null;
    // This function deletes middle nodes 
    // in a sequence of horizontal and
    // vertical line segments represented
    // by linked list.
    Node deleteMiddle()
        // If only one node or no node...
        // Return back
        if (head == null || 
   == null ||
   == null)
            return head;
        Node Next =;
        Node NextNext =;
        // Check if this is vertical or 
        // horizontal line
        if (head.x == Next.x)
            // Find middle nodes with same value 
            // as x and delete them.
            while (NextNext != null && 
                   Next.x == NextNext.x)
       = null;
                // Update NextNext for the next iteration
                Next = NextNext;
                NextNext =;
        // if horizontal
        else if (head.y == Next.y)
            // find middle nodes with same value as y and
            // delete them
            while (NextNext != null && Next.y == NextNext.y)
       = null;
                // Update NextNext for the next iteration
                Next = NextNext;
                NextNext =;
        // Adjacent points should have same x or same y
            System.out.println("Given list is not valid");
            return null;
        // recur for other segment
        // temporarily store the head and move head forward.
        Node temp = head;
        head =;
        // call deleteMiddle() for next segment
        // restore head
        head = temp;
        // return the head
        return head;
    /*  Given a reference (pointer to pointer) to the head
        of a list and an int, push a new node on the front
        of the list. */
    void push(int x, int y)
        /* 1 & 2: Allocate the Node &
                  Put in the data*/
        Node new_node = new Node(x,y);
        /* 3. Make next of new Node as head */ = head;
        /* 4. Move the head to point to new Node */
        head = new_node;
    void printList()
        Node temp = head;
        while (temp != null)
            temp =;
    /* Driver program to test above functions */
    public static void main(String args[])
        LinkedList llist = new LinkedList();
        System.out.println("Given list");
        if (llist.deleteMiddle() != null)
            System.out.println("Modified Linked List is");
} /* This code is contributed by Rajat Mishra */


Given Linked List:
(0,10)-> (1,10)-> (3,10)-> (10,10)-> (10,8)-> (10,5)-> (20,5)-> (40,5)->
Modified Linked List:
(0,10)-> (10,10)-> (10,5)-> (40,5)-> 

Time Complexity of the above solution is O(n) where n is a number of nodes in the given linked list.
The above code is recursive, write an iterative code for the same problem. Please see below for the solution.
Iterative approach for removing middle points in a linked list of line segments
Please refer complete article on Given a linked list of line segments, remove middle points for more details!

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!