Linked list problem statement
Learn the most efficient way to search an element in a doubly linked list. The linked list is one of the most important concepts and data structures to learn while preparing for interviews. Having a good grasp of Linked Lists can be a huge plus point in a coding interview. Problem StatementIn this problem, we are given a Doubly Linked List, and we need to search for a node with value X in the list and return its position. Problem Statement Understanding:Let’s try to understand the problem statement with the help of an example. If the given doubly linked list is head ↔ 4 ↔ 5 ↔ 7 ↔ 6 ↔ 2 ↔ 1 ↔ 9 and X = 7.
Input: head ↔ 4 ↔ 5 ↔ 7 ↔ 6 ↔ 2 ↔ 1 ↔ 9, X(element to be searched) = 7 Also, there can be multiple nodes with value X in the list, so that's why we will return the position of the first node with value X in the list. Note: If there is no node with the value of X in the list, the output will be -1. Now I think the problem statement is clear, so let’s see how we can approach it. Let’s move to the approach section. ApproachIn order to find an element X in the given doubly linked list:
Algorithm
Dry RunCode Implementation
#include using namespace std; struct DLLNode { int data; DLLNode* next; DLLNode* prev; }; void push(DLLNode** head, int new_data){ DLLNode* new_node = (DLLNode*)malloc(sizeof(struct DLLNode)); new_node->data = new_data; new_node->prev = NULL; new_node->next = (*head); if ((*head) != NULL) { (*head)->prev = new_node; } (*head) = new_node; } int search_the_element(DLLNode** head, int X){ DLLNode* temp = *head; int position = 0; while (temp->data != X && temp->next != NULL) { position++; temp = temp->next; } if (temp->data != X) return -1; return (position + 1); } int main() { DLLNode* head = NULL; int X = 7; push(&head, 9); push(&head, 1); push(&head, 2); push(&head, 6); push(&head, 7); push(&head, 5); push(&head, 4); cout< public class Prepbytes { static class Node { int data; Node next; Node prev; }; static Node push(Node head_ref, int new_data) { Node new_node = new Node(); new_node.data = new_data; new_node.prev = null; new_node.next = head_ref; if (head_ref != null) { head_ref.prev = new_node; } head_ref = new_node; return head_ref; } static int search(Node head_ref, int x) { Node temp = head_ref; int position = 1; while (temp.data != x && temp.next != null) { position++; temp = temp.next; } if (temp.data != x) return -1; return position; } public static void main(String[] args) { Node head = null; int X = 7; head = push(head, 9); head = push(head, 1); head = push(head, 2); head = push(head, 6); head = push(head, 7); head = push(head, 5); head = push(head, 4); System.out.print(search(head, X)); } } Output3 Time Complexity: O(n), where n is the total number of nodes in the Doubly Linked List. So, in this blog, we have tried to explain how you can search a value in a doubly linked list. If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link Linked List. Given a singly linked list, group all odd nodes together followed by all the even nodes. Please note here we are talking about the node number and not the value in the nodes. The program should run in O(1) space complexity and O(nodes) time complexity.
Given a circular linked list, you have to count the number of nodes in it. For example: A Circular linked list with a total of 6 nodes.SolutionWell, there is only one way to solve the problem, and that is to traverse the circular linked list keeping the track of the number of nodes as we traverse. Nothing really fancy, right? In this tutorial, we will show you how we can solve the problem using straight-forward way i.e traversing and counting, and then we will also see how we can design a recursive solution to solve the problem, overkill, I know, but still, there’s no harm to know different ways to solve a problem, right? Continue reading “Count the number of nodes in a circular linked list”
Given a sorted linked list and a node, insert the node in the linked list such that the linked list still remain sorted. For Example Input: 2 -> 8 -> 12 -> 41 ->NULL, val = 30
Given a linked list, write a program to delete it. For Example Input: 2->4->6->9->NULL *After the Deletion* Output: The Linked List is Empty.
Assume the structure of a Linked List node is as follows.
Explain the functionality of the following C functions. 1. What does the following function do for a given Linked List?
fun1() prints the given Linked List in the reverse way. For Linked List 1->2->3->4->5, fun1() prints 5->4->3->2->1. 2. What does the following function do for a given Linked List?
fun2() prints alternate nodes of the given Linked List, first from head to end, and then from end to head. If Linked List has even number of nodes, then fun2() skips the last node. For Linked List 1->2->3->4->5, fun2() prints 1 3 5 5 3 1. For Linked List 1->2->3->4->5->6, fun2() prints 1 3 5 5 3 1. Below is a complete running program to test the above functions.
Output: Output of fun1() for list 1->2->3->4->5 5 4 3 2 1 Output of fun2() for list 1->2->3->4->5 1 3 5 5 3 1Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above. Article Tags : Practice Tags : |