Given a linked list of line segments, remove middle points
Last Updated :
21 Aug, 2022
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.
C++
#include <bits/stdc++.h>
using namespace std;
class Node
{
public :
int x, y;
Node *next;
};
void push(Node ** head_ref, int x, int y)
{
Node* new_node = new Node();
new_node->x = x;
new_node->y = y;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(Node *head)
{
Node *temp = head;
while (temp != NULL)
{
cout << "(" << temp->x << "," << temp->y << ")-> " ;
temp = temp->next;
}
cout<<endl;
}
void deleteNode(Node *head, Node *Next)
{
head->next = Next->next;
Next->next = NULL;
free (Next);
}
Node* deleteMiddle(Node *head)
{
if (head == NULL || head->next == NULL ||
head->next->next == NULL)
return head;
Node* Next = head->next;
Node *NextNext = Next->next ;
if (head->x == Next->x)
{
while (NextNext != NULL && Next->x == NextNext->x)
{
deleteNode(head, Next);
Next = NextNext;
NextNext = NextNext->next;
}
}
else if (head->y==Next->y)
{
while (NextNext != NULL && Next->y == NextNext->y)
{
deleteNode(head, Next);
Next = NextNext;
NextNext = NextNext->next;
}
}
else
{
puts ( "Given linked list is not valid" );
return NULL;
}
deleteMiddle(head->next);
return head;
}
int main()
{
Node *head = NULL;
push(&head, 40,5);
push(&head, 20,5);
push(&head, 10,5);
push(&head, 10,8);
push(&head, 10,10);
push(&head, 3,10);
push(&head, 1,10);
push(&head, 0,10);
cout << "Given Linked List: \n" ;
printList(head);
if (deleteMiddle(head) != NULL);
{
cout << "Modified Linked List: \n" ;
printList(head);
}
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int x, y;
struct Node *next;
};
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;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList( struct Node *head)
{
struct Node *temp = head;
while (temp != NULL)
{
printf ( "(%d,%d)-> " , temp->x,temp->y);
temp = temp->next;
}
printf ( "\n" );
}
void deleteNode( struct Node *head, struct Node *Next)
{
head->next = Next->next;
Next->next = NULL;
free (Next);
}
struct Node* deleteMiddle( struct Node *head)
{
if (head==NULL || head->next ==NULL || head->next->next==NULL)
return head;
struct Node* Next = head->next;
struct Node *NextNext = Next->next ;
if (head->x == Next->x)
{
while (NextNext !=NULL && Next->x==NextNext->x)
{
deleteNode(head, Next);
Next = NextNext;
NextNext = NextNext->next;
}
}
else if (head->y==Next->y)
{
while (NextNext !=NULL && Next->y==NextNext->y)
{
deleteNode(head, Next);
Next = NextNext;
NextNext = NextNext->next;
}
}
else
{
puts ( "Given linked list is not valid" );
return NULL;
}
deleteMiddle(head->next);
return head;
}
int main()
{
struct Node *head = NULL;
push(&head, 40,5);
push(&head, 20,5);
push(&head, 10,5);
push(&head, 10,8);
push(&head, 10,10);
push(&head, 3,10);
push(&head, 1,10);
push(&head, 0,10);
printf ( "Given Linked List: \n" );
printList(head);
if (deleteMiddle(head) != NULL);
{
printf ( "Modified Linked List: \n" );
printList(head);
}
return 0;
}
|
Java
class LinkedList
{
Node head;
class Node
{
int x,y;
Node next;
Node( int x, int y)
{
this .x = x;
this .y = y;
next = null ;
}
}
Node deleteMiddle()
{
if (head == null || head.next == null ||
head.next.next == null )
return head;
Node Next = head.next;
Node NextNext = Next.next;
if (head.x == Next.x)
{
while (NextNext != null && Next.x == NextNext.x)
{
head.next = Next.next;
Next.next = null ;
Next = NextNext;
NextNext = NextNext.next;
}
}
else if (head.y == Next.y)
{
while (NextNext != null && Next.y == NextNext.y)
{
head.next = Next.next;
Next.next = null ;
Next = NextNext;
NextNext = NextNext.next;
}
}
else
{
System.out.println( "Given list is not valid" );
return null ;
}
Node temp = head;
head = head.next;
this .deleteMiddle();
head = temp;
return head;
}
void push( int x, int y)
{
Node new_node = new Node(x,y);
new_node.next = head;
head = new_node;
}
void printList()
{
Node temp = head;
while (temp != null )
{
System.out.print( "(" +temp.x+ "," +temp.y+ ")->" );
temp = temp.next;
}
System.out.println();
}
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();
}
}
}
|
Python3
class LinkedList( object ):
def __init__( self ):
self .head = None
class Node( object ):
def __init__( self , x, y):
self .x = x
self .y = y
self . next = None
def deleteMiddle( self ):
if self .head = = None or self .head. next = = None or self .head. next . next = = None :
return self .head
Next = self .head. next
NextNext = Next . next
if self .head.x = = Next .x:
while NextNext ! = None and Next .x = = NextNext.x:
self .head. next = Next . next
Next . next = None
Next = NextNext
NextNext = NextNext. next
elif self .head.y = = Next .y:
while NextNext ! = None and Next .y = = NextNext.y:
self .head. next = Next . next
Next . next = None
Next = NextNext
NextNext = NextNext. next
else :
print ( "Given list is not valid" )
return None
temp = self .head
self .head = self .head. next
self .deleteMiddle()
self .head = temp
return self .head
def push( self , x, y):
new_node = self .Node(x, y)
new_node. next = self .head
self .head = new_node
def printList( self ):
temp = self .head
while temp ! = None :
print ( "(" + str (temp.x) + "," + str (temp.y) + ")->" ,end = " " )
temp = temp. next
print ()
llist = 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 )
print ( "Given list" )
llist.printList()
if llist.deleteMiddle() ! = None :
print ( "Modified Linked List is" )
llist.printList()
|
C#
using System;
public class LinkedList
{
Node head;
class Node
{
public int x,y;
public Node next;
public Node( int x, int y)
{
this .x = x;
this .y = y;
next = null ;
}
}
Node deleteMiddle()
{
if (head == null || head.next == null ||
head.next.next == null )
return head;
Node Next = head.next;
Node NextNext = Next.next;
if (head.x == Next.x)
{
while (NextNext != null &&
Next.x == NextNext.x)
{
head.next = Next.next;
Next.next = null ;
Next = NextNext;
NextNext = NextNext.next;
}
}
else if (head.y == Next.y)
{
while (NextNext != null && Next.y == NextNext.y)
{
head.next = Next.next;
Next.next = null ;
Next = NextNext;
NextNext = NextNext.next;
}
}
else
{
Console.WriteLine( "Given list is not valid" );
return null ;
}
Node temp = head;
head = head.next;
this .deleteMiddle();
head = temp;
return head;
}
void push( int x, int y)
{
Node new_node = new Node(x,y);
new_node.next = head;
head = new_node;
}
void printList()
{
Node temp = head;
while (temp != null )
{
Console.Write( "(" +temp.x + "," + temp.y + ")->" );
temp = temp.next;
}
Console.WriteLine();
}
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);
Console.WriteLine( "Given list" );
llist.printList();
if (llist.deleteMiddle() != null )
{
Console.WriteLine( "Modified Linked List is" );
llist.printList();
}
}
}
|
Javascript
<script>
var head;
class Node {
constructor(x , y) {
this .x = x;
this .y = y;
this .next = null ;
}
}
function deleteMiddle() {
if (head == null || head.next == null ||
head.next.next == null )
return head;
var Next = head.next;
var NextNext = Next.next;
if (head.x == Next.x) {
while (NextNext != null &&
Next.x == NextNext.x)
{
head.next = Next.next;
Next.next = null ;
Next = NextNext;
NextNext = NextNext.next;
}
}
else if (head.y == Next.y) {
while (NextNext != null &&
Next.y == NextNext.y) {
head.next = Next.next;
Next.next = null ;
Next = NextNext;
NextNext = NextNext.next;
}
}
else {
document.write( "Given list is not valid" );
return null ;
}
var temp = head;
head = head.next;
this .deleteMiddle();
head = temp;
return head;
}
function push(x , y) {
var new_node = new Node(x, y);
new_node.next = head;
head = new_node;
}
function printList() {
var temp = head;
while (temp != null ) {
document.write( "(" + temp.x + "," +
temp.y + ")->" );
temp = temp.next;
}
document.write( "<br/>" );
}
push(40, 5);
push(20, 5);
push(10, 5);
push(10, 8);
push(10, 10);
push(3, 10);
push(1, 10);
push(0, 10);
document.write( "Given list<br/>" );
printList();
if (deleteMiddle() != null ) {
document.write( "Modified Linked List is<br/>" );
printList();
}
</script>
|
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
Share your thoughts in the comments
Please Login to comment...