Category Archives: Linked Lists

Linked List problems

Decimal Equivalent of Binary Linked List

Given a singly linked list of 0s and 1s find its decimal equivalent. Input : 0->0->0->1->1->0->0->1->0 Output : 50 Input : 1->0->0 Output : 4 Decimal Value of an empty linked list is considered as 0. Initialize result as 0. Traverse the linked list and for each node, multiply the result by 2 and add… Read More »

Merge K sorted linked lists

Given K sorted linked lists of size N each, merge them and print the sorted output. Example: Input: k = 3, n = 4 list1 = 1->3->5->7->NULL list2 = 2->4->6->8->NULL list3 = 0->9->10->11 Output: 0->1->2->3->4->5->6->7->8->9->10->11   Method 1 (Simple) A Simple Solution is to initialize result as first list. Now traverse all lists starting from… Read More »

Check if a linked list is Circular Linked List

Given a singly linked list, find if the linked list is circular or not. A linked list is called circular if it not NULL terminated and all nodes are connected in the form of a cycle. Below is an example of circular linked list. An empty linked list is considered as circular. Note that this… Read More »

Delete middle of linked list

Given a singly linked list, delete middle of the linked list. For example, if given linked list is 1->2->3->4->5 then linked list should be modified to 1->2->4->5 If there are even nodes, then there would be two middle nodes, we need to delete the second middle element. For example, if given linked list is 1->2->3->4->5->6… Read More »

Create a Doubly Linked List from a Ternary Tree

Given a ternary tree, create a doubly linked list out of it. A ternary tree is just like binary tree but instead of having two nodes, it has three nodes i.e. left, middle, right. The doubly linked list should holds following properties – Left pointer of ternary tree should act as prev pointer of doubly… Read More »