From 0acfe71b68803658e87e3499be7e97d378ee72d8 Mon Sep 17 00:00:00 2001 From: sunilsalat <69308623+sunilsalat@users.noreply.github.com> Date: Thu, 21 Apr 2022 10:59:36 +0530 Subject: [PATCH] Update 3_linked_list.py --- data_structures/3_LinkedList/3_linked_list.py | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/data_structures/3_LinkedList/3_linked_list.py b/data_structures/3_LinkedList/3_linked_list.py index a6d9466..e6e49df 100644 --- a/data_structures/3_LinkedList/3_linked_list.py +++ b/data_structures/3_LinkedList/3_linked_list.py @@ -29,19 +29,21 @@ def get_length(self): def insert_at_begining(self, data): node = Node(data, self.head) + + if self.head is None: + self.head = node + self.tail = node + return + node.next = self.head self.head = node def insert_at_end(self, data): if self.head is None: - self.head = Node(data, None) - return - - itr = self.head - - while itr.next: - itr = itr.next - - itr.next = Node(data, None) + self.insert_at_begining(data) + return + node = Node(data) + self.tail.next = node + self.tail = node def insert_at(self, index, data): if index<0 or index>self.get_length():
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: