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++
#include <iostream>
using namespace std;
struct Node {
int x, y;
struct Node *next;
};
void push( struct Node **head_ref, int x, int y)
{
struct Node *new_node = new 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 delete_Middle_Nodes(Node *head)
{
Node *temp = head->next, *prev = head;
while (temp) {
if (temp->x == prev->x)
{
Node *curr = prev;
prev = temp;
temp = temp->next;
while (temp && temp->x == prev->x)
{
curr->next = temp;
free (prev);
prev = temp;
temp = temp->next;
}
}
else if (temp->y == prev->y)
{
Node *curr = prev;
prev = temp;
temp = temp->next;
while (temp && temp->y == prev->y)
{
curr->next = temp;
free (prev);
prev = temp;
temp = temp->next;
}
} else {
prev = temp;
temp = temp->next;
}
}
}
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);
delete_Middle_Nodes(head);
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 ;
}
}
private void delete_Middle_Nodes(Node head)
{
Node prev = head;
Node temp = head.next;
while (temp != null )
{
if (temp.x == prev.x)
{
Node curr = prev;
prev = temp;
temp = temp.next;
while (temp != null && temp.x == prev.x)
{
curr.next = temp;
prev.next = null ;
prev = temp;
temp = temp.next;
}
}
else if (temp.y == prev.y)
{
Node curr = prev;
prev = temp;
temp = temp.next;
while (temp != null && temp.y == prev.y)
{
curr.next = temp;
prev.next = null ;
prev = temp;
temp = temp.next;
}
}
else
{
prev =temp;
temp = temp.next;
}
}
}
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();
llist.delete_Middle_Nodes(llist.head);
System.out.println( "Modified Linked List is" );
llist.printList();
}
}
|
Python3
import math
class Node:
def __init__( self , x, y):
self .x = x
self .y = y
self . next = None
def push(head_ref, x, y):
new_node = Node(x, y)
new_node.x = x
new_node.y = y
new_node. next = head_ref
head_ref = new_node
return head_ref
def printList(head):
temp = head
while (temp ! = None ):
print ( "(" , temp.x, "," ,
temp.y, ")" , end = "->" ,)
temp = temp. next
print ()
def delete_Middle_Nodes(head):
temp = head. next
prev = head
while (temp):
if (temp.x = = prev.x):
curr = prev
prev = temp
temp = temp. next
while (temp ! = None and temp.x = = prev.x):
curr. next = temp
prev = temp
temp = temp. next
elif (temp.y = = prev.y):
curr = prev
prev = temp
temp = temp. next
while (temp ! = None and temp.y = = prev.y):
curr. next = temp
prev = temp
temp = temp. next
else :
prev = temp
temp = temp. next
if __name__ = = '__main__' :
head = None
head = push(head, 40 , 5 )
head = push(head, 20 , 5 )
head = push(head, 10 , 5 )
head = push(head, 10 , 8 )
head = push(head, 10 , 10 )
head = push(head, 3 , 10 )
head = push(head, 1 , 10 )
head = push(head, 0 , 10 )
print ( "Given Linked List: \n" , end = "")
printList(head)
delete_Middle_Nodes(head)
print ( "Modified Linked List: \n" , end = "")
printList(head)
|
C#
using System;
public class LinkedList
{
public Node head;
public class Node
{
public int x,y;
public Node next;
public Node( int x, int y)
{
this .x = x;
this .y = y;
next = null ;
}
}
private void delete_Middle_Nodes(Node head)
{
Node prev = head;
Node temp = head.next;
while (temp != null )
{
if (temp.x == prev.x)
{
Node curr = prev;
prev = temp;
temp = temp.next;
while (temp != null && temp.x == prev.x)
{
curr.next = temp;
prev.next = null ;
prev = temp;
temp = temp.next;
}
}
else if (temp.y == prev.y)
{
Node curr = prev;
prev = temp;
temp = temp.next;
while (temp != null && temp.y == prev.y)
{
curr.next = temp;
prev.next = null ;
prev = temp;
temp = temp.next;
}
}
else
{
prev =temp;
temp = temp.next;
}
}
}
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();
llist.delete_Middle_Nodes(llist.head);
Console.WriteLine( "Modified Linked List is" );
llist.printList();
}
}
|
Javascript
<script>
var head = null ;
class Node
{
constructor(x, y)
{
this .x =x;
this .y = y;
this .next = null ;
}
}
function delete_Middle_Nodes(head)
{
var prev = head;
var temp = head.next;
while (temp != null )
{
if (temp.x == prev.x)
{
var curr = prev;
prev = temp;
temp = temp.next;
while (temp != null && temp.x == prev.x)
{
curr.next = temp;
prev.next = null ;
prev = temp;
temp = temp.next;
}
}
else if (temp.y == prev.y)
{
var curr = prev;
prev = temp;
temp = temp.next;
while (temp != null && temp.y == prev.y)
{
curr.next = temp;
prev.next = null ;
prev = temp;
temp = temp.next;
}
}
else
{
prev =temp;
temp = temp.next;
}
}
}
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();
delete_Middle_Nodes(head);
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: O(N) where N is no of nodes in given linked list
Auxiliary Space: O(1)
Last Updated :
27 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...