Basic Problems on Linked Lists

In this section we will go through some of the basic programming problems involving linked lists.

Search for a value in Linked List

Linked lists are made up of a sequence of nodes where each node links to the next one, forming a chain-like structure. Searching for a value in a linked list involves visiting each node in the list in sequence, access its value and checking if it is equal to the desired value. We continue doing this until reaching the end of the list or until we find the desired node.

Read More

Merging Multiple Linked Lists into a Single Linked List

Merging multiple linked lists involves combining two or more linked lists into a single, consolidated linked list. This operation is crucial for efficiently managing and manipulating large data which requires frequent regrouping or reordering. One example use case is in text editors, where text is often represented as a collection of linked lists (e.g., one list per line or paragraph) and these lists are merged when converting the document to plain text or when performing operations like search and replace across the entire document.

Read More

Finding the Middle Node(s) in a Linked List Data Structure

Finding the middle node(s) in a linked list involves identifying the element(s) present at the center of a linked list data structure. This operation is crucial in various applications, such as efficiently splitting a linked list into two halves, optimizing certain algorithms that require balanced partitioning of data etc.

Read More

Reverse a linked list (iterative and recursive approaches)

A linked list consists of nodes where each node contains an item/value along with a reference to next node in the sequence. Reversing a linked list involves changing the direction of the links for each node, effectively flipping the linked list from its original order to the opposite. This article will explore both iterative and recursive methods to reverse a linked list using Python.

Read More

Remove duplicates from a sorted linked list

Removing duplicate elements from a sorted linked list involves eliminating nodes with repeated occurrences of similar elements while maintaining the sorted order of the list.

In order to remove duplicates, we traverse through the linked list one node at a time while checking is the next node’s value is same a current node. If the next node’s value is same as the current node’s value, we skip/delete the next node and point the current node’s reference to next next node.

Read More

Merge two sorted linked lists into one sorted linked list

Merging two sorted linked lists involves combining all nodes present in each of the two input linked lists into a single linked list while maintaining their sorted order.

To accomplish this, we need consume the smallest head node among both the linked lists and add it to the final list. We continue the process until all nodes in both the linked lists are added to the merged sorted linked list. This approach ensures that the new merged list preserves the sorted order while re-using all nodes from the original two lists.

Read More