Javascript 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.
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(n) due to recursive stack 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!
Share your thoughts in the comments
Please Login to comment...