Flatten a Multi-Level Linked List in Depth-first way

Problem Given a linked list where each node has a next pointer and a child pointer that can point to a separate, nested list, flatten the list so that all nodes appear in a single-level, depth-first order. Examples Example 1: Input: head = [1, [2, [5, 6] ], 3, 4] 1 -> 2 -> 3 -> 4 | 5 -> 6 Output: 1 -> 2 -> 5 -> 6 -> 3 -> 4 ...

September 15, 2024 · 3 min · TagsList of tags for the post  linkedlist

Delete Nodes From Linked List Present in Array Problem

Problem You are given an array of integers nums and the head of a linked list. Return the head of the modified linked list after removing all nodes from the linked list that have a value that exists in nums. Examples Example 1: graph LR A[1]:::toBeDeleted --> B[2]:::toBeDeleted --> C[3]:::toBeDeleted --> D[4] --> E[5] classDef toBeDeleted fill:#ffcccc,stroke:#ff0000; ...

Find the Minimum and Maximum Number of Nodes Between Critical Points Problem

Problem A critical point in a linked list is defined as either a local maxima or a local minima. A node is a local maxima if the current node has a value strictly greater than the previous node and the next node. A node is a local minima if the current node has a value strictly smaller than the previous node and the next node. Note that a node can only be a local maxima/minima if there exists both a previous node and a next node. ...

Merge Nodes in Between Zeros Problem

Problem You are given the head of a linked list, which contains a series of integers separated by 0’s. The beginning and end of the linked list will have Node.val == 0. For every two consecutive 0’s, merge all the nodes lying in between them into a single node whose value is the sum of all the merged nodes. The modified list should not contain any 0’s. Return the head of the modified linked list. ...

Split Linked List in Parts Problem

Problem Given the head of a singly linked list and an integer k, split the linked list into k consecutive linked list parts. The length of each part should be as equal as possible: no two parts should have a size differing by more than one. This may lead to some parts being null. The parts should be in the order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal to parts occurring later. ...

Find length of the linked list

Problem Given a linked list, return the length or size of the linked list. Examples Input: head=[1, 2, 8, 3, 7, 0, 4] Output : 7 Explanation: Given linkedlist has 7 nodes Solution Method 1 - Recursive Code Java class Solution { public int size(ListNode head) { // Base case: if the head is null, return 0 if (head == null) { return 0; } // Recursive case: 1 + length of the rest of the list return 1 + size(head.next); } } ...

Double a Number Represented as a Linked List

Double a Number Represented as a Linked List Problem You are given the head of a non-empty linked list representing a non-negative integer without leading zeroes. Return the head of the linked list after doubling it. Examples Example 1: --- title: Input --- graph LR; 1 --> 8 --> 9 --- title: Output --- graph LR; 3 --> 7 --> 8 ...

Linked List in Binary Tree Problem

Problem Given a binary tree root and a linked list with head as the first node. Return True if all the elements in the linked list starting from the head correspond to some downward path connected in the binary tree otherwise return False. In this context downward path means a path that starts at some node and goes downwards. Examples Example 1: ...

Merge K Sorted Lists

Problem You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it. Examples 1 -> 10 -> 20 4 -> 11 -> 13 3 -> 8 -> 9 will result in 1 -> 3 -> 4 -> 8 -> 9 -> 10 -> 11 -> 13 -> 20 ...

Odd Even Linked List - Bring odd nodes first and even later

Problem Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list. The first node is considered odd, and the second node is even, and so on. Note that the relative order inside both the even and odd groups should remain as it was in the input. You must solve the problem in O(1) extra space complexity and O(n) time complexity. ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy