# Given a linked list of line segments, remove middle points

Given a linked list of co-ordinates 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.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

Following are C/C++ and Java implementations of above idea.

## C/C++

```// C program to remove intermediate points in a linked list
// that represents horizontal and vertical line segments
#include <stdio.h>
#include <stdlib.h>

// Node has 3 fields including x, y coordinates and a pointer
// to next node
struct Node
{
int x, y;
struct Node *next;
};

/* Function to insert a node at the beginning */
void push(struct Node ** head_ref, int x,int y)
{
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));
new_node->x  = x;
new_node->y  = y;
}

/* Utility function to print a singly linked list */
{
while (temp != NULL)
{
printf("(%d,%d)-> ", temp->x,temp->y);
temp = temp->next;
}
printf("\n");

}

// Utility function to remove Next from linked list
void deleteNode(struct Node *head, struct Node *Next)
{
Next->next = NULL;
free(Next);
}

// This function deletes middle nodes in a sequence of
// horizontal and vertical line segments represented by
{
// If only one node or no node...Return back

struct Node *NextNext = Next->next ;

// Check if this is a vertical line or horizontal line
{
// Find middle nodes with same x value, and delete them
while (NextNext !=NULL && Next->x==NextNext->x)
{

// Update Next and NextNext for next iteration
Next = NextNext;
NextNext = NextNext->next;
}
}
else if (head->y==Next->y) // If horizontal line
{
// Find middle nodes with same y value, and delete them
while (NextNext !=NULL && Next->y==NextNext->y)
{

// Update Next and NextNext for next iteration
Next = NextNext;
NextNext = NextNext->next;
}
}
else  // Adjacent points must have either same x or same y
{
puts("Given linked list is not valid");
return NULL;
}

// Recur for next segment

}

// Driver program to tsst above functions
int main()
{

{
}
return 0;
}
```

## Java

```// Java program to remove middle points in a linked list of
// line segments,
{

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
Node deleteMiddle()
{
// If only one node or no node...Return back

Node NextNext = Next.next;

// check if this is vertical or horizontal line
{
// Find middle nodes with same value as x and
// delete them.
while (NextNext != null && Next.x == NextNext.x)
{
Next.next = null;

// Update NextNext for the next iteration
Next = NextNext;
NextNext = NextNext.next;
}
}

// if horizontal
{
// find middle nodes with same value as y and
// delete them
while (NextNext != null && Next.y == NextNext.y)
{
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

// call deleteMiddle() for next segment
this.deleteMiddle();

}

/*  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 */

/* 4. Move the head to point to new Node */
}

void printList()
{
while (temp != null)
{
System.out.print("("+temp.x+","+temp.y+")->");
temp = temp.next;
}
System.out.println();
}

/* Drier program to test above functions */
public static void main(String args[])
{

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)
{
llist.printList();
}
}
} /* This code is contributed by Rajat Mishra */
```

## Python

```
# Python program to remove middle points in a linked list of
# line segments,
def __init__(self):

class Node(object):
def __init__(self, x, y):
self.x = x
self.y = y
self.next = None

# This function deletes middle nodes in a sequence of
# horizontal and vertical line segments represented
def deleteMiddle(self):
# If only one node or no node...Return back
NextNext = Next.next
# check if this is vertical or horizontal line
# Find middle nodes with same value as x and
# delete them.
while NextNext != None and Next.x == NextNext.x:
Next.next = None
# Update NextNext for the next iteration
Next = NextNext
NextNext = NextNext.next
# find middle nodes with same value as y and
# delete them
while NextNext != None and Next.y == NextNext.y:
Next.next = None
# Update NextNext for the next iteration
Next = NextNext
NextNext = NextNext.next
else:
# Adjacent points should have same x or same y
print "Given list is not valid"
return None
# recur for other segment
# call deleteMiddle() for next segment
self.deleteMiddle()

# 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.
def push(self, x, y):
# 1 & 2: Allocate the Node &
# Put in the data
new_node = self.Node(x, y)
# 3. Make next of new Node as head
# 4. Move the head to point to new Node

def printList(self):
while temp != None:
print "(" + str(temp.x) + "," + str(temp.y) + ")->",
temp = temp.next
print ''

# Driver program
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)

print "Given list"
llist.printList()

if llist.deleteMiddle() != None:
llist.printList()

# This code is contributed by BHAVYA JAIN
```

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

Time Complexity of the above solution is O(n) where n is number of nodes in given linked list.

Exercise:
The above code is recursive, write an iterative code for the same problem.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
2.9 Average Difficulty : 2.9/5.0
Based on 84 vote(s)