Open In App

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

Last Updated : 21 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.
Examples: 

Input:   (0,10)->(1,10)->(5,10)->(7,10)
                                  |
                                (7,5)->(20,5)->(40,5)
Output: Linked List should be changed to following
        (0,10)->(7,10)
                  |
                (7,5)->(40,5) 
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
    (2,3)->(12,3) 
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




// 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 ||
            head.next == null ||
            head.next.next == null)
            return head;
 
        Node Next = head.next;
        Node NextNext = Next.next;
 
        // 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)
            {
                head.next = Next.next;
                Next.next = null;
 
                // Update NextNext for the next iteration
                Next = NextNext;
                NextNext = NextNext.next;
            }
        }
 
        // 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)
            {
                head.next = Next.next;
                Next.next = null;
 
                // Update NextNext for the next iteration
                Next = NextNext;
                NextNext = NextNext.next;
            }
        }
 
        // Adjacent points should have same x or same y
        else
        {
            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 = head.next;
 
        // call deleteMiddle() for next segment
        this.deleteMiddle();
 
        // 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 */
        new_node.next = head;
 
        /* 4. Move the head to point to new Node */
        head = new_node;
    }
 
 
    void printList()
    {
        Node temp = head;
        while (temp != null)
        {
            System.out.print("("+temp.x+","+temp.y+")->");
            temp = temp.next;
        }
        System.out.println();
    }
 
 
    /* Driver program to test above functions */
    public static void main(String args[])
    {
        LinkedList llist = new LinkedList();
 
        llist.push(40,5);
        llist.push(20,5);
        llist.push(10,5);
        llist.push(10,8);
        llist.push(10,10);
        llist.push(3,10);
        llist.push(1,10);
        llist.push(0,10);
 
        System.out.println("Given list");
        llist.printList();
 
        if (llist.deleteMiddle() != null)
        {
            System.out.println("Modified Linked List is");
            llist.printList();
        }
    }
} /* This code is contributed by Rajat Mishra */


Output: 

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.

Auxiliary Space: O(1) because it is using constant space

Exercise: 
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!



Similar Reads

C++ Program For Removing Middle Points From a Linked List Of Line Segments
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.Examples: Input: (0,10)->(1,10)->(5,10)->(7,10) | (7,5)->(20,5)->(40,5) Output: Linked List should be changed to following (0,10)->(7
4 min read
C Program For Removing Middle Points From a Linked List Of Line Segments
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.Examples: Input: (0,10)->(1,10)->(5,10)->(7,10) | (7,5)->(20,5)->(40,5) Output: Linked List should be changed to following (0,10)->(7
4 min read
Python Program For Removing Middle Points From a Linked List Of Line Segments
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.Examples: Input: (0,10)->(1,10)->(5,10)->(7,10) | (7,5)->(20,5)->(40,5) Output: Linked List should be changed to following (0,10)->(7
4 min read
Javascript Program For Removing Middle Points From a Linked List Of Line Segments
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.Examples: Input: (0,10)->(1,10)->(5,10)->(7,10) | (7,5)->(20,5)->(40,5) Output: Linked List should be changed to following (0,10)->(7
4 min read
Iterative approach for removing middle points in a linked list of line segments
This post explains the iterative approach of this problem. We maintain two pointers, prev and temp. If these two have either x or y same, we move forward till the equality holds and keep deleting the nodes in between. The node from which the equality started, we adjust the next pointer of that node. Implementation: C/C++ Code // C++ program to remo
9 min read
Given a linked list of line segments, remove middle points
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. Examples: Input: (0,10)->(1,10)->(5,10)->(7,10) | (7,5)->(20,5)->(40,5) Output: Linked List should be changed to following (0,10)->(
14 min read
Given n line segments, find if any two segments intersect
Given n line segments (p1, q1), (p2, q2), ... (pn, qn), find if the given line segments intersect with each other or not. Example: Input: {{1, 5}, {4, 5}}, {{2, 5}, {10, 1}},{{3, 2}, {10, 3}},{{6, 4}, {9, 4}},{{7, 1}, {8, 1}}Output: 2Explanation: Lines {{1, 5}, {4, 5}}, {{2, 5}, {10, 1}} are intersecting, as well as {{2, 5}, {10, 1}},{{3, 2}, {10,
10 min read
Number of horizontal or vertical line segments to connect 3 points
Given three points on the x-y coordinate plane. You need to find the no. of line segments formed by making a polyline passing through these points. (Line segment can be vertically or horizontally aligned to the coordinate axis) Examples : Input : A = {-1, -1}, B = {-1, 3}, C = {4, 3} Output : 2 Expantaion: There are two segments in this polyline. I
8 min read
Minimum points to be selected such that removal of line segments passing through them empties given array
Given a 2D array arr[][], where each row is of the form {start, end} representing the start and endpoints of each line segment on the X-axis. In one step, select a point on the X-axis and delete all the line segments passing through that point. The task is to find the minimum number of such points that need to be selected to delete all the line seg
7 min read
Equation of straight line passing through a given point which bisects it into two equal line segments
Given a straight line which passes through a given point (x0, y0) such that this point bisects the line segment in two equal line segments. The task is to find the equation of this straight line.Examples: Input: x0 = 4, y0 = 3 Output: 3x + 4y = 24Input: x0 = 7, y0 = 12 Output: 12x + 7y = 168 Approach: Let PQ be the line and AB be the line segment b
4 min read
Practice Tags :