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 ...

Swap Every Kth Node in a LinkedList Problem

Problem Given a linked list, swap every kth node in that. If at the end of the list remaining nodes are less than k, leave them untouched. Input: A linked list, A number k. Examples Example 1: --- title: Input List with k = 4 --- graph LR A1[1] --> B2[2] --> C3[3] --> D4[4]:::kth_node --> E5[5] --> F6[6] --> G7[7] --> H8[8]:::kth_node --> I9[9] --> J10[10] classDef kth_node fill:#FFA500,stroke:#000,stroke-width:2px; ...

Recover Binary Search Tree Problem

Recover Binary Search Tree Problem Problem You are given the root of a binary search tree (BST), where the values of exactly two nodes of the tree were swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? Example Example 1: Input: *1 / *3 \ 2 Output: *3 / *1 \ 2 ...

Swap Kth node from beginning with Kth node from end in a Linked List

Problem You are given the head of a linked list, and an integer k. Return the head of the linked list after swapping the values of the kth node from the beginning and the kth node from the end (the list is 1-indexed). OR Given a Linked List and a number k, Swap Kth Node from the front with the Kth Node from the End Examples Example 1: ...

Swap alternate nodes in a singly linked list

Problem Given a single Linked List. Swap every other alternate nodes. Examples Example 1: Input: head = 1 -> 2 -> 3 -> 4 -> 5 -> null Output: 3 -> 4 -> 5 -> 1 -> 2 -> null Explanation: The list was transformed in several steps. 1. First step, swap 1 and 3 i.e. 3–>2–>1–>4–>5–>null 2. Second step, swap 2 and 4 i.e. 3–>4–>1–>2–>5–>null 3. last step, swap 1 and 5, i.e. 3–>4–>5–>2–>1–>null ...

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