Partition List Problem

Problem Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. Examples Example 1: --- title: Input --- graph LR A1[1]:::less --> B4[4]:::greater --> C3[3]:::pivot --> D2[2]:::less --> E5[5]:::greater --> F2[2]:::less classDef pivot fill:#ffb6c1,stroke:#000,stroke-width:2px; classDef less fill:#ffcc00,stroke:#000,stroke-width:2px; classDef greater fill:#add8e6,stroke:#000,stroke-width:2px; ...

Swap Nodes in Pairs Problem

Problem Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list’s nodes (i.e., only nodes themselves may be changed.) Examples Example 1: --- title: Input --- graph LR; 1 --> 2:::focus --> 3 -->4:::focus classDef focus fill:#f96 --- title: Output --- graph LR; 2:::focus --> 1 --> 4:::focus -->3 classDef focus fill:#f96 ...

Delete the Middle Node of a Linked List Problem

Problem You are given the head of a linked list. Delete the middle node, and return the head of the modified linked list. The middle node of a linked list of size n is the ⌊n / 2⌋th node from the start using 0-based indexing, where ⌊x⌋ denotes the largest integer less than or equal to x. For n = 1, 2, 3, 4, and 5, the middle nodes are 0, 1, 1, 2, and 2, respectively. Examples Example 1: ...

Add Two Numbers represented as Linked List

Problem You are given two linked lists representing two non-negative numbers. Problem 1 - The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 ...

Split Linked List in alternating way

Problem Given a singly linked list, split it into two linked lists. These linked lists will contain the alternate nodes from the given linked list. Examples Example 1: head = [1,2,3,4,5] Output: [ [1, 3, 5], [2, 4] ] ...

Copy List with Random Pointer Problem

Problem A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. i.e. Return a deep copy of the list. OR A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null. Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list. ...

Delete X Nodes After Y Nodes

Problem Given a Linked List and x and y. Delete x number of nodes after y nodes from the start. Examples Example 1: --- title: Input list with x = 5 and y = 3 --- graph LR A[1] --> B[2] --> C[3] --> D[4]:::delete --> E[5]:::delete --> F[6]:::delete --> G[7]:::delete --> H[8]:::delete --> I[9] --> J[10] classDef delete fill:#ffcccc,stroke:#ff0000,stroke-width:2px; ...

April 14, 2023 · 3 min · TagsList of tags for the post  linkedlist

Flatten a Multi-Level Linked List level by level

Problem Given a linked list, in addition to the next pointer, each node has a child pointer that can point to a separate list. With the head node, flatten the list to a single-level linked list. Examples Example 1: Input: head = [1, [2, [5, 6] ], 3, 4] 1 -> 2 -> 3 -> 4 | 5 -> 6 Output: 1 -> 2 -> 3 -> 4 -> 5 -> 6 ...

Intersection of Two Linked Lists Problem

Problem Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null. Intersection point means end of one linked list is linked with some node in another linked list and it forms a Y shape. Input: Two Linked List Output: Intersection Node or point, find Intersection Point in Two Linked List ...

K reverse a Linked List in alternating way

Problem Given a singly linked list and a number k, write an algorithm to reverse every alternate k nodes in the linked list. If there are fewer than k nodes left at the end, leave them as is. Examples Example 1: --- title: Input --- graph LR A1[1] --> B2[2] --> C3[3] --> D4[4] --> E5[5] --> F6[6] --> G7[7] --> H8[8] classDef swapped fill:#ffcc00,stroke:#000,stroke-width:2px; ...

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