There are two singly linked lists in a system. By some programming error the end node of one of the linked list got linked into the second list, forming a inverted Y shaped list. Write a program to get the point where two linked list merge.
Algorithm: Let input linked list is sorted in increasing order.
Asked by Varun Bhatia.
You are given a Double Link List with one pointer of each node pointing to the next node just like in a single link list. The second pointer however CAN point to any node in the list and not just the previous node. Now write a program in O(n) time to duplicate this list. That… Read More »
Asked by Varun Bhatia. Question: Write a recursive function treeToList(Node root) that takes an ordered binary tree and rearranges the internal pointers to make a circular doubly linked list out of the tree nodes.
Given a singly linked list of characters, write a function that returns true if the given list is palindrome, else false. METHOD 1 (Use a Stack) A simple solution is to use a stack of list nodes. This mainly involves three steps. 1) Traverse the given list from head to tail and push every visited… Read More »
Given a linked list, check if the the linked list has loop or not. Below diagram shows a linked list with a loop.
Given pointer to the head node of a linked list, the task is to reverse the linked list. Examples: Input : Head of following linked list 1->2->3->4->NULL Output : Linked list should be changed to, 4->3->2->1->NULL Input : Head of following linked list 1->2->3->4->5->NULL Output : Linked list should be changed to, 5->4->3->2->1->NULL Input :… Read More »
A simple solution is to traverse the linked list until you find the node you want to delete. But this solution requires pointer to the head node which contradicts the problem statement.
Given a singly linked list and a key, count number of occurrences of given key in linked list. For example, if given linked list is 1->2->1->2->1->3->1 and given key is 1, then output should be 4. We strongly recommend that you click here and practice it, before moving on to the solution. Algorithm: 1. Initialize… Read More »
Algorithm For C/C++: Iterate through the linked list and delete all the nodes one by one. Main point here is not to access next of the current pointer if current pointer is deleted. In Java, automatic garbage collection happens, so deleting a linked list is easy. We just need to change head to null. Implementation:… Read More »