Java Program For Removing Middle Points From a Linked List Of Line Segments
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.
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();
}
}
}
|
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
Java Program For Removing Every K-th Node Of The Linked List
Given a singly linked list, Your task is to remove every K-th node of the linked list. Assume that K is always less than or equal to length of Linked List.Examples : Input: 1->2->3->4->5->6->7->8 k = 3 Output: 1->2->4->5->7->8 As 3 is the k-th node after its deletion list would be 1->2->4->5->6->7->
4 min read
Java Program For Removing Duplicates From A Sorted Linked List
Write a function that takes a list sorted in non-decreasing order and deletes any duplicate nodes from the list. The list should only be traversed once. For example if the linked list is 11->11->11->21->43->43->60 then removeDuplicates() should convert the list to 11->21->43-
8 min read
Java Program For Removing All Occurrences Of Duplicates From A Sorted Linked List
Given a sorted linked list, delete all nodes that have duplicate numbers (all occurrences), leaving only numbers that appear once in the original list. Examples: Input: 23->28->28->35->49->49->53->53 Output: 23->35 Input: 11->11->11->11->75->75 Output: empty List Note that this is different from Rem
3 min read
Java Program For Removing Duplicates From An Unsorted Linked List
Given an unsorted Linked List, the task is to remove duplicates from the list. Examples: Input: linked_list = 12 -> 11 -> 12 -> 21 -> 41 -> 43 -> 21 Output: 12 -> 11 -> 21 -> 41 -> 43 Explanation: Second occurrence of 12 and 21 are removed. Input: linked_list = 12 ->
6 min read
Java Program to Delete a Node From the Middle of the Circular Linked List
In this article, we are going to learn to delete the middle node from the circular Linked List in java. The approach we are going to follow for this program is, first we calculate the number of nodes in the list and then divide the number of nodes by 2 to get the middle node of the list. Algorithm C
4 min read
Java Program For Inserting Node In The Middle Of The Linked List
Given a linked list containing n nodes. The problem is to insert a new node with data x at the middle of the list. If n is even, then insert the new node after the (n/2)th node, else insert the new node after the (n+1)/2th node. Examples: Input : list: 1->2->4->5 x = 3 Output : 1->2->
5 min read
Java Program To Delete Middle Of Linked List
Given a singly linked list, delete the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 1->2->4->5 If there are even nodes, then there would be two middle nodes, we need to delete the second middle element. For example, if g
4 min read
Java Program For Finding Intersection Point Of Two Linked Lists
There are two singly linked lists in a system. By some programming error, the end node of one of the linked lists got linked to the second list, forming an inverted Y-shaped list. Write a program to get the point where two linked lists merge. Above diagram shows an example with two linked lists havi
7 min read
Java Program to Reverse a Linked List Without Manipulating its Pointers
Given a linked list, the task is to write a program in Java that reverses the linked list without manipulating its pointers, i.e., the reversal should happen just by changing the data values and not the links. ExamplesInput: Original linked list 1->2->3->4->5->nullOutput: Linked list
3 min read
Java Program For Moving Last Element To Front Of A Given Linked List
Write a function that moves the last element to the front in a given Singly Linked List. For example, if the given Linked List is 1->2->3->4->5, then the function should change the list to 5->1->2->3->4. Algorithm: Traverse the list till the last node. Use two pointers: one t
3 min read