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....

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....

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....

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....

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....

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....

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....

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....

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....

August 31, 2024