From 23290feec073e5b76009b13479d6632919d64dbc Mon Sep 17 00:00:00 2001 From: Anushri Date: Thu, 6 Nov 2025 12:19:22 -0500 Subject: [PATCH 01/11] exercise-1 without max length --- Exercise_1.py | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..ee64e61f6 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,20 +1,45 @@ + +# // Did this code successfully run on Leetcode : Yes on Geeksforgeeks +# // Any problem you faced while coding this : Yes + + +# // Your code here along with comments explaining your approach + class myStack: #Please read sample.java file before starting. #Kindly include Time and Space complexity at top of each file def __init__(self): - + self.stacklist = [] + +# // Time Complexity : O(1) +# // Space Complexity : def isEmpty(self): - + return not self.stacklist + +# // Time Complexity : O(1) +# // Space Complexity : O(n) def push(self, item): - + self.stacklist.append(item) + +# // Time Complexity : O(1) +# // Space Complexity : def pop(self): - - + return self.stacklist.pop() + +# // Time Complexity : O(1) +# // Space Complexity : def peek(self): - + return self.stacklist[-1] + +# // Time Complexity : O(1) +# // Space Complexity : def size(self): - + return len(self.stacklist) + +# // Time Complexity : O(1) +# // Space Complexity : def show(self): + return self.stacklist s = myStack() From b0423e4213f43361ffb9746329fbd65ea9255e32 Mon Sep 17 00:00:00 2001 From: Anushri Date: Fri, 7 Nov 2025 16:01:12 -0500 Subject: [PATCH 02/11] exercise-1 --- Exercise_1.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index ee64e61f6..381a6d969 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,7 +1,8 @@ -# // Did this code successfully run on Leetcode : Yes on Geeksforgeeks -# // Any problem you faced while coding this : Yes - +# Time Complexity : O(1) +# Space Complexity : O(n) +# Did this code successfully run on Leetcode : Yes on Geeksforgeeks +# Any problem you faced while coding this : No # // Your code here along with comments explaining your approach From 72ce675fc988182a86bad9dc0a3e70309e5ff22b Mon Sep 17 00:00:00 2001 From: Anushri Date: Fri, 7 Nov 2025 18:19:28 -0500 Subject: [PATCH 03/11] code working for push and not for pop --- Exercise_2.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..b5ce29dfa 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -6,10 +6,31 @@ def __init__(self, data): class Stack: def __init__(self): - + self.current_node = None + self.previous_node = None + def push(self, data): + if self.current_node: + self.previous_node = self.current_node + + self.current_node = Node(data) + + if self.previous_node: + self.previous_node.next = self.current_node + def pop(self): + if self.current_node: + popped = self.current_node + print(self.previous_node.data) + if self.previous_node: + self.current_node = self.previous_node + self.previous_node = None + else: + self.current_node = None + return popped.data + return None + a_stack = Stack() while True: From 0c468d1ee0f113f529c6209f7c2302c50894c630 Mon Sep 17 00:00:00 2001 From: Anushri Date: Fri, 7 Nov 2025 18:25:15 -0500 Subject: [PATCH 04/11] exercise-2 approach 1 added comments --- Exercise_2.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Exercise_2.py b/Exercise_2.py index b5ce29dfa..f5de68359 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -9,6 +9,7 @@ def __init__(self): self.current_node = None self.previous_node = None +# push is working def push(self, data): if self.current_node: self.previous_node = self.current_node @@ -18,14 +19,13 @@ def push(self, data): if self.previous_node: self.previous_node.next = self.current_node - +# pop is not working def pop(self): if self.current_node: popped = self.current_node print(self.previous_node.data) if self.previous_node: self.current_node = self.previous_node - self.previous_node = None else: self.current_node = None return popped.data From 62255ef4736dd478e365e83be0155a5b5302fafe Mon Sep 17 00:00:00 2001 From: Anushri Date: Sat, 8 Nov 2025 08:52:27 -0500 Subject: [PATCH 05/11] exrcise-1 aprroach first node address into second node --- Exercise_2.py | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/Exercise_2.py b/Exercise_2.py index f5de68359..4a7cee651 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -6,33 +6,30 @@ def __init__(self, data): class Stack: def __init__(self): - self.current_node = None - self.previous_node = None + self.top = None -# push is working + # push is working def push(self, data): - if self.current_node: - self.previous_node = self.current_node - - self.current_node = Node(data) + new_node = Node(data) + # new node is an object and it contains the data - if self.previous_node: - self.previous_node.next = self.current_node + if self.top: + # if there is something in the stack new_node's pointer will point to that top + new_node.next = self.top + # if there is nothing already then new node become top + self.top = new_node -# pop is not working + # pop is working def pop(self): - if self.current_node: - popped = self.current_node - print(self.previous_node.data) - if self.previous_node: - self.current_node = self.previous_node - else: - self.current_node = None + if self.top: + popped = self.top + self.top = popped.next return popped.data return None a_stack = Stack() + while True: #Give input as string if getting an EOF error. Give input like "push 10" or "pop" print('push ') From 68bb8d3bac02bf0d0d9e39a012d62c61c05f3e4f Mon Sep 17 00:00:00 2001 From: Anushri Date: Sat, 8 Nov 2025 18:15:49 -0500 Subject: [PATCH 06/11] with comments --- Exercise_2.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Exercise_2.py b/Exercise_2.py index 4a7cee651..86732a237 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,8 +1,8 @@ class Node: - def __init__(self, data): + def __init__(self, data, next): self.data = data - self.next = None + self.next = next class Stack: def __init__(self): @@ -11,18 +11,20 @@ def __init__(self): # push is working def push(self, data): new_node = Node(data) - # new node is an object and it contains the data - + # if there is something in the stack if self.top: - # if there is something in the stack new_node's pointer will point to that top + # new_node's pointer will point to that top new_node.next = self.top # if there is nothing already then new node become top self.top = new_node # pop is working def pop(self): + # if top is present if self.top: + # popped the top and store into the popped variable popped = self.top + # link the top to the next pointer of the popped self.top = popped.next return popped.data return None From f9a61336784cf7835dc70c611139d569a356d7d1 Mon Sep 17 00:00:00 2001 From: Anushri Date: Sat, 8 Nov 2025 18:17:35 -0500 Subject: [PATCH 07/11] exercise-3 --- Exercise_3.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..b41c71cdd 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,8 +1,15 @@ +# Time Complexity : O(n) +# Space Complexity : O(n) +# Did this code successfully run on Leetcode : Yes on Geeksforgeeks +# Any problem you faced while coding this : I faced difficulty while writing a logic for append and find class ListNode: """ A node in a singly-linked list. """ def __init__(self, data=None, next=None): + # initilize the data and next attribute + self.data = data + self.next = next class SinglyLinkedList: def __init__(self): @@ -10,6 +17,7 @@ def __init__(self): Create a new singly-linked list. Takes O(1) time. """ + # initializing head with None self.head = None def append(self, data): @@ -17,6 +25,27 @@ def append(self, data): Insert a new element at the end of the list. Takes O(n) time. """ + # creating an object + next_node = ListNode(data) + # first node will be head + # second node will linked to next of the first node + # third node will linked to next of the second node + # fourth node will linked to next of the third node + if self.head: + # line 32 to 35 will find last node in the linked list + # can not iterate on head because we don't want to change the head of the linked list + node = self.head + # iterating over the node untill we find the last node which will have null in its next + while node.next: + # incrementing the node to check the null + node = node.next + # this node is the last node in the linked list. we are linking the new node with this + node.next = next_node + else: + # first node will be the head + self.head = next_node + + def find(self, key): """ @@ -24,9 +53,53 @@ def find(self, key): `key`. Return the element or `None` if not found. Takes O(n) time. """ + if self.head: + node = self.head + # iterating over the entire linked list to find the matching key + while node: + if node.data == key: + return node + node = node.next + # if there's nothing in the list and no matching element is found + return None def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + # iterating over the entire linked list to find the matching key + if self.head: + node = self.head + previous_node = None + # iterating over the entire linked list to find the matching key + while node: + # checking key with the list data + if node.data == key: + if previous_node: + if node.next: + # we have both previous and next, so we are removing middle element and previous is pointing to next + previous_node.next = node.next + # when there is no next element the previous node will point to None + else: + previous_node.next = None + else: + self.head = node.next + previous_node = node + node = node.next + return None + +s_link = SinglyLinkedList() +s_link.append(3) +s_link.append(4) +# s_link.remove(4) +# s_link.remove(3) +# print(s_link.head.next) +print(s_link.find(4)) +print(s_link.find(2)) +# print(s_link.head) +node = s_link.head +while node: + print(node) + node = node.next + From c8d6c30b220f8899d3bdd508386a699376b9a731 Mon Sep 17 00:00:00 2001 From: Anushri Date: Sat, 8 Nov 2025 18:19:33 -0500 Subject: [PATCH 08/11] exercise-2 with comments --- Exercise_2.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Exercise_2.py b/Exercise_2.py index 86732a237..ed3f218da 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,4 +1,7 @@ - +# Time Complexity : O(1) +# Space Complexity : O(n) +# Did this code successfully run on Leetcode : Yes on Geeksforgeeks +# Any problem you faced while coding this : I faced difficulty while writing a logic for append and find class Node: def __init__(self, data, next): self.data = data @@ -8,7 +11,6 @@ class Stack: def __init__(self): self.top = None - # push is working def push(self, data): new_node = Node(data) # if there is something in the stack @@ -18,7 +20,6 @@ def push(self, data): # if there is nothing already then new node become top self.top = new_node - # pop is working def pop(self): # if top is present if self.top: From 674c10d2661632825d8feff2c6d95ac9fd7b9527 Mon Sep 17 00:00:00 2001 From: Anushri Date: Sat, 8 Nov 2025 21:15:22 -0500 Subject: [PATCH 09/11] exercise-2 --- Exercise_2.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Exercise_2.py b/Exercise_2.py index ed3f218da..545c34b6a 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -3,9 +3,9 @@ # Did this code successfully run on Leetcode : Yes on Geeksforgeeks # Any problem you faced while coding this : I faced difficulty while writing a logic for append and find class Node: - def __init__(self, data, next): + def __init__(self, data): self.data = data - self.next = next + self.next = None class Stack: def __init__(self): From a316ff9f4a76a479bb2537b84226a9bd418b5760 Mon Sep 17 00:00:00 2001 From: Anushri Date: Mon, 10 Nov 2025 17:39:51 -0500 Subject: [PATCH 10/11] Exercise_2 Implement Stack using Linked List --- Exercise_2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Exercise_2.py b/Exercise_2.py index 545c34b6a..b936a1252 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,7 +1,7 @@ # Time Complexity : O(1) # Space Complexity : O(n) # Did this code successfully run on Leetcode : Yes on Geeksforgeeks -# Any problem you faced while coding this : I faced difficulty while writing a logic for append and find +# Any problem you faced while coding this : Faced difficulty while writing a logic for pop class Node: def __init__(self, data): self.data = data From b546f53cdf456ffeee90cb295c0003f5a673a1c9 Mon Sep 17 00:00:00 2001 From: Anushri Date: Mon, 10 Nov 2025 17:45:28 -0500 Subject: [PATCH 11/11] Exercise_2 Implement Stack using Linked List --- Exercise_3.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Exercise_3.py b/Exercise_3.py index b41c71cdd..7a31625fe 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,7 +1,7 @@ # Time Complexity : O(n) # Space Complexity : O(n) -# Did this code successfully run on Leetcode : Yes on Geeksforgeeks -# Any problem you faced while coding this : I faced difficulty while writing a logic for append and find +# Did this code successfully run on Leetcode : No. Could not find the problem +# Any problem you faced while coding this : Faced difficulty while writing a logic for append and find class ListNode: """ A node in a singly-linked list.