Circular list | add at middle | Sololearn: Learn to code for FREE!


Circular list | add at middle

Can we still optimize the code to add element at middle of circular list ? Appreciate your feedback on below code:

6/1/2021 3:44:46 PM

Ketan Lalcheta

4 Answers

New Answer


Maybe you could globally define a length for the list? Doing that will prevent the use of another loop to determine the bounds of the current list. You can keep track of the length by initializing a counter and incrementing/decrementimg it whenever you add/remove an item. Hopefully this helps


Yeah nice one... Probably making length of list as mylist class data member


You can also find the middle by using a classical 2 pointer approach. You can do that by traversing the list using two pointers. Move one pointer by one and the other pointers by two ( double the speed ). When the fast pointer reaches end, slower one would already be pointing at mid.


Thanks Choe nd Arsenic