Iterative approach for removing middle points in a linked list of line segements

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.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to remove intermediate 
// points in a linked list that represents
// horizontal and vertical line segments
#include <iostream>
using namespace std;
  
// 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 = new Node;   
    new_node->x = x;
    new_node->y = y;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}
  
/* Utility function to print a singly linked list */
void printList(struct Node *head)
{
    struct Node *temp = head;    
    while (temp != NULL) {
        printf("(%d, %d)-> ", temp->x, temp->y);        
        temp = temp->next;
    }    
    printf("\n");
}
  
// This function deletes middle nodes in a 
// sequence of horizontal and vertical line 
// segments represented by linked list.
void delete_Middle_Nodes(Node *head) 
{    
    Node *temp = head->next, *prev = head;  
    
    while (temp) {
          
        // checking equlity of point x
        if (temp->x == prev->x) 
        {
            Node *curr = prev;
            prev = temp;
            temp = temp->next;        
              
            // removing vertical points of line 
            // segment from linked list
            while (temp && temp->x == prev->x) 
            {
                curr->next = temp;
                free(prev);
                prev = temp;
                temp = temp->next;
            }
        }
  
        // checking equlity of point y
        else if (temp->y == prev->y) 
        {
            Node *curr = prev;
            prev = temp;
            temp = temp->next;
              
            // removing horizontal points of line 
            // segment from linked list
            while (temp && temp->y == prev->y)
            {
                curr->next = temp;
                free(prev);
                prev = temp;
                temp = temp->next;
            }
        } else {
            prev = temp;
            temp = temp->next;
        }
    }
}
  
// Driver program to test above functions
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

class LinkedList
{
    Node head; // head of list
  
    /* 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.
    private void delete_Middle_Nodes(Node head)
    {
        Node prev = head;
        Node temp = head.next;
  
        while (temp != null)
        {
  
            // checking equlity of point x
            if (temp.x == prev.x)
            {
                Node curr = prev;
                prev = temp;
                temp = temp.next;
  
                // removing vertical points of line 
                // segment from linked list
                while (temp != null && temp.x == prev.x)
                {
                    curr.next = temp;
                    prev.next = null;
                    prev = temp;
                    temp = temp.next;
                }
            }
  
            // checking equlity of point y 
            else if (temp.y == prev.y)
            {
                Node curr = prev;
                prev = temp;
                temp = temp.next;
  
                // removing horizontal points of line 
                // segment from linked list
                while (temp != null && temp.y == prev.y)
                {
                    curr.next = temp;
                    prev.next = null;
                    prev = temp;
                    temp = temp.next;
                }
            
            else
            {
                prev =temp;
                temp = temp.next;
            }
        }
    }
  
    /* 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 code */
    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();
    }
}
  
// This code is contributed by shubham96301.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to remove intermediate 
// points in a linked list that represents
// horizontal and vertical line segments
using System;
      
public class LinkedList
{
    public Node head; // head of list
  
    /* Linked list Node*/
    public class Node
    {
        public int x,y;
        public Node next;
        public 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.
    private void delete_Middle_Nodes(Node head)
    {
        Node prev = head;
        Node temp = head.next;
  
        while (temp != null)
        {
  
            // checking equlity of point x
            if (temp.x == prev.x)
            {
                Node curr = prev;
                prev = temp;
                temp = temp.next;
  
                // removing vertical points of line 
                // segment from linked list
                while (temp != null && temp.x == prev.x)
                {
                    curr.next = temp;
                    prev.next = null;
                    prev = temp;
                    temp = temp.next;
                }
            }
  
            // checking equlity of point y 
            else if (temp.y == prev.y)
            {
                Node curr = prev;
                prev = temp;
                temp = temp.next;
  
                // removing horizontal points of line 
                // segment from linked list
                while (temp != null && temp.y == prev.y)
                {
                    curr.next = temp;
                    prev.next = null;
                    prev = temp;
                    temp = temp.next;
                }
            
            else
            {
                prev =temp;
                temp = temp.next;
            }
        }
    }
  
    /* 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)
        {
            Console.Write("(" + temp.x + "," + temp.y + ")->");
            temp = temp.next;
        }
        Console.WriteLine();
    }
  
  
    /* Driver code */
    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();
    }
}
  
/* This code contributed by PrinciRaj1992 */

chevron_right


Output:

Given Linked List: 
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)-> 


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.