Data Structures in Python: Circular Linked Lists -- Split List

preview_player
Показать описание
In this video, we investigate how to split one circular linked list into two separate circular linked lists. We code up the solution in Python.

The software written in this video is available at:

Do you like the development environment I'm using in this video? It's a customized version of vim that's enhanced for Python development. If you want to see how I set up my vim, I have a series on this here:

If you've found this video helpful and want to stay up-to-date with the latest videos posted on this channel, please subscribe:
Рекомендации по теме
Комментарии
Автор

sir can you please explain me this;
in line 2 in while loop you have it is while cur and cur<mid... I tried using only while cur<mid it works.. what is the need of curr also ..
thanks for content

athanikarammy
Автор

Sir, please upload graph datastructures as well

ezlearn
Автор

I have been watching your videos for over a week now and I've learned so much! I'm attempting to do the code for myself first, and for calculating the length of the list, I used the following code:
def __len__(self):
cur = self.head
count = 1
while cur.next != self.head:
count += 1
cur = cur.next
return count
It seems to work but I wonder if you have any feedback on my approach vs. yours?

kimber
Автор

Thanks !!!
I tried to code a function that doesn't change the original list, but returns two new lists:


def split_list(self):
list_1 = CircularLinkedList()
list_2 = CircularLinkedList()

length =
mid_position = length//2 + 1
count = 1
current_node = self.head

while count != mid_position:

current_node = current_node.next
count += 1

while current_node:
if current_node == self.head:
break

current_node = current_node.next

return list_1, list_2


what do you think about it??

mulhamjarjanazi
Автор

at 12:27, do we need to append it separately if we put a condition while(cur != self.head)?

dharmendrameena
Автор

here's my __len__ override method


def __len__(self):
count, node = 1, self.head.next
while node != self.head:
count += 1
node = node.next
return count

ian_senior
Автор

i have a doubt what is while cur: doing?? please help

MsKshitij
Автор

6:54 No sir, we are not skeptical 😂😂😂😂😂😂

yashpandey
Автор

why are we appending the elements of second list, I mean when we split them off from the initial list they are already connected to each other and already forming a linked list, we just have to point the split_list's object to its head and then make it circular by giving the last node reference to the head.


Please make me clear and have a look at the following code..


def split_list(self):
size = len(self)

if size == 0 :
return None
if size ==1:
return self.head

mid = size // 2
count = 1
cur = self.head

while count != mid:
count +=1
cur =cur.next
sec_list = CircularList()
sec_list.head = cur.next
temp = sec_list.head


while temp.next != self.head:
temp = temp.next
temp.next = sec_list.head
cur.next = self.head

prachinainawa
Автор

Nice job ..

But the second list dont seem to be a circular one. Its head and last node next pointers seem to be missing.

mvihaan
Автор

why self.head = self.head.next is a must?

sutingyang