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 »
Given a Linked List and a number n, write a function that returns the value at the n’th node from end of the Linked List.
Given a singly linked list, find middle of the linked list. For example, if given linked list is 1->2->3->4->5 then output should be 3. If there are even nodes, then there would be two middle nodes, we need to print second middle element. For example, if given linked list is 1->2->3->4->5->6 then output should be… Read More »
A simple solution is to traverse the linked list until you find the node you want to delete.
Write a GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position.