Basic linked list

Here is the link to the problem: Basic Linked List Creating a linked list in Python involves defining a structure to store each element (node) and then linking each node to the next one. Here’s a simple step-by-step explanation: Define the Node Class: Each node in a linked list contains some data and a reference to the next node in the list. In Python, we can create a Node class to represent each element. class Node: def __init__(self, data): self.data = data # Store the data self.next = None # Reference to the next node (initially None) Define the LinkedList Class: We create a LinkedList class that manages the nodes. This class will have a head attribute pointing to the first node in the list. class LinkedList: def __init__(self): self.head = None # Start with an empty list (no head node yet) Add Methods to the LinkedList: Let’s add a method to insert data at the end of the list. def append(self, data): new_node = Node(data) # Create a new node if self.head is None: # If the list is empty, make this the head node self.head = new_node return last = self.head while last.next: # Traverse to the end of the list last = last.next last.next = new_node # Link the last node to the new node Display the List: We can add a method to print all the elements in the list. def display(self): current = self.head while current: # Traverse through the list print(current.data, end=" -> ") current = current.next print("None") # End of the list Using the Linked List: Now, we can create a linked list, add elements to it, and display it. # Create a LinkedList ll = LinkedList() ll.append(1) ll.append(2) ll.append(3) # Display the LinkedList ll.display() Output: 1 -> 2 -> 3 -> None In summary: ...

October 30, 2024

Remove duplicates from sorted linked list

Here is the link to the problem: Remove duplicates from sorted linked list Problem Statement with Thought Process: Here’s a Python function to remove duplicates from a sorted linked list: class ListNode: def __init__(self, value=0, next=None): self.value = value self.next = next def remove_duplicates(head: ListNode) -> ListNode: current = head while current and current.next: if current.value == current.next.value: current.next = current.next.next # Skip the duplicate node else: current = current.next # Move to the next unique node return head Explanation The function takes the head of a sorted linked list as input. It iterates through the list, checking if the current node has the same value as the next node. If they are the same, it skips the next node by pointing current.next to current.next.next. If they differ, it simply moves to the next node. Example Usage # Helper function to print the list def print_list(head: ListNode): current = head while current: print(current.value, end=" -> ") current = current.next print("None") # Create a sorted linked list with duplicates head = ListNode(1, ListNode(1, ListNode(2, ListNode(3, ListNode(3))))) print("Original list:") print_list(head) # Remove duplicates head = remove_duplicates(head) print("List after removing duplicates:") print_list(head) Output Original list: 1 -> 1 -> 2 -> 3 -> 3 -> None List after removing duplicates: 1 -> 2 -> 3 -> None This function efficiently removes duplicates from a sorted linked list in O(n) time, where ( n ) is the number of nodes in the list. ...

October 28, 2024

Remove duplicates from sorted linked list

Here is the link to the problem: Remove duplicates from sorted linked list Problem Statement with Thought Process: Here’s a Python function to remove duplicates from a sorted linked list: class ListNode: def __init__(self, value=0, next=None): self.value = value self.next = next def remove_duplicates(head: ListNode) -> ListNode: current = head while current and current.next: if current.value == current.next.value: current.next = current.next.next # Skip the duplicate node else: current = current.next # Move to the next unique node return head Explanation The function takes the head of a sorted linked list as input. It iterates through the list, checking if the current node has the same value as the next node. If they are the same, it skips the next node by pointing current.next to current.next.next. If they differ, it simply moves to the next node. Example Usage # Helper function to print the list def print_list(head: ListNode): current = head while current: print(current.value, end=" -> ") current = current.next print("None") # Create a sorted linked list with duplicates head = ListNode(1, ListNode(1, ListNode(2, ListNode(3, ListNode(3))))) print("Original list:") print_list(head) # Remove duplicates head = remove_duplicates(head) print("List after removing duplicates:") print_list(head) Output Original list: 1 -> 1 -> 2 -> 3 -> 3 -> None List after removing duplicates: 1 -> 2 -> 3 -> None This function efficiently removes duplicates from a sorted linked list in O(n) time, where ( n ) is the number of nodes in the list. ...

October 28, 2024

Find middle node in linked list

Here is the link to the problem: Middle node in linked list. Problem Statement with Thought Process: There are three approaches to this probles: Just traverse the whole list and divide by half? We will discuss the second method Third method is just like second method but here, we only increment count if the pointer is in odd node. Basically the same thing as jumping a node right? The two-pointer approach to find the middle of a linked list works because one pointer moves at double the speed of the other. Here’s how and why it works: ...

October 26, 2024

Find the intersection Node in the linked list

Here is the link to the problem: Intersection node in the linked list. Problem Statement with Thought Process: To find the intersection point between two linked lists in Python, we can use the approach you described: Calculate the lengths of both lists. Find the absolute difference in lengths and move the pointer of the longer list ahead by that difference. Move both pointers forward one node at a time until they meet, which is the intersection point. Implementation class ListNode: def __init__(self, value=0, next=None): self.value = value self.next = next def get_length(head): length = 0 while head: length += 1 head = head.next return length def get_intersection_node(headA, headB): # Calculate the lengths of both linked lists lenA = get_length(headA) lenB = get_length(headB) # Find the difference in lengths diff = abs(lenA - lenB) # Advance the pointer for the longer list by 'diff' nodes if lenA > lenB: for _ in range(diff): headA = headA.next else: for _ in range(diff): headB = headB.next # Move both pointers forward until they meet at the intersection node while headA and headB: if headA == headB: return headA # Intersection point headA = headA.next headB = headB.next return None # No intersection # Example usage # Create linked lists with an intersection point for testing # A: 1 -> 2 -> 3 \ # 6 -> 7 # B: 4 -> 5 / a = ListNode(1) a.next = ListNode(2) a.next.next = ListNode(3) a.next.next.next = ListNode(6) a.next.next.next.next = ListNode(7) b = ListNode(4) b.next = ListNode(5) b.next.next = a.next.next.next # Set intersection at node with value 6 intersection = get_intersection_node(a, b) print("Intersection at node with value:", intersection.value if intersection else "No intersection") Complexity Time Complexity: O(m+n) ...

October 26, 2024

Reverse a linked list (Iterative way)

Here is the link to the problem: Reverse a linked list (Iterative way). Problem Statement with Thought Process: To reverse a linked list iteratively, we can use three pointers: Previous Pointer: Keeps track of the previous node, which will help us reverse the links. Current Pointer: Points to the current node we’re processing. Next Pointer: Temporarily stores the next node before we change the current node’s link. Here’s the iterative process: ...

October 26, 2024

Reverse a linked list (Recursive way)

Here is the link to the problem: Reverse a linked list (Recursive way). Problem Statement with Thought Process: To reverse a linked list recursively, we can use the following approach: Base Case: If we reach the end of the list (i.e., head is None) or there is only one node left, we return head as it will be the new head of the reversed list. Recursive Step: Recursively reverse the rest of the list. Set the next node’s next pointer to the current node (reversing the link). Set the current node’s next pointer to None to prevent cycles. Here’s the Python code for reversing a linked list recursively: ...

October 26, 2024

Find if there is a cycle in linked list

Here is the link to the problem: cycle in linked list Problem Statement with Thought Process: We need to find if there is a cycle in linked list Implementation There are two pointers p1 and p2. P2 will jump by 1 node. P1 will just go serially. If there is a cycle, at one time, it is guaranteed that both of them will meet at some point. This approach describes Floyd’s Cycle Detection Algorithm, also known as the “Tortoise and Hare” algorithm. Here’s an implementation in Python: ...

October 25, 2024

Find if two linked list have intersection point

Here is the link to the problem: Linked list intersection point. Problem Statement with Thought Process: There are two ways to do this: Using flag: In this method, we set a flag to 0 in each node. After traversal to the node, the flag is changed to 1. P1 comes doing this for first ll(linked list) and P2 comes doing this for second ll. If there is already a node with 1 as the flag, that means that is the intersection point. But this will take more space to store the flag. ...

August 31, 2024

Find the kth Node from the Back

Here is the link to the problem: kth node from Back. Problem Statement with Thought Process: We need to find the kth node from the back of the linked list. Imagine your four finger, where index finger is P1 and pinky finger is P2. To find 4th node from back, we place our hand at the end of the linked list, the index finger points to the 4th node from back. We took pinky finger kth steps from P1 and then moved P2 to the end of the list. Answer is pointed by P1. ...

August 31, 2024