Python Program For Removing Middle Points From a Linked List Of Line Segments
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.
Python
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) + ")->" ,
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()
|
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!
Last Updated :
21 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...