Author: akshay
Problem Statement: Design and implement a custom singly linked list class with the following operations: Operations: get(index): Return the value of the node at the specified index (0-indexed). If index is invalid, return -1. addAtHead(val): Insert a node at the beginning of the list. addAtTail(val): Append a node at the end of the list. addAtIndex(index, val): Insert a node before the index-th node in the list. deleteAtIndex(index): Delete the node at the given index. Approach: Use a custom Node class that holds val and next attributes. Maintain a head pointer to the first node and a size to track the…
Problem Statement: 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: Input: head = [1,2,3,4]Output: [2,1,4,3] Input: head = []Output: [] Input: head = [1]Output: [1] Constraints: The number of nodes in the list is in the range [0, 100]. 0 ≤ Node.val ≤ 100 Approach (Recursive): Base case: if the list is empty or has only one node, return the head. Let l be the first node and r be the second. Call swapPairs…
Problem Statement: 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: Input: head = [1,2,3,4]Output: [2,1,4,3] Input: head = []Output: [] Input: head = [1]Output: [1] Input: head = [1,2,3]Output: [2,1,3] Constraints: The number of nodes in the list is in the range [0, 100]. 0 ≤ Node.val ≤ 100 Approach: Use a dummy node before the head to handle edge cases easily. Use pointers to swap pairs by rewiring node connections. Iterate through the…
Problem Statement: Given the head of a linked list, rotate the list to the right by k places. Examples: Input: head = [1,2,3,4,5], k = 2Output: [4,5,1,2,3] Input: head = [0,1,2], k = 4Output: [2,0,1] Constraints: The number of nodes in the list is in the range [0, 500]. -100 ≤ Node.val ≤ 100 0 ≤ k ≤ 2 * 109 Approach: First, compute the length of the list. Adjust k using modulo: k = k % length. Use two pointers: move one k steps ahead, then move both until the first reaches the end. Rewire the pointers to rotate…
Problem Statement: You are given the heads of two sorted linked lists list1 and list2. Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list. Examples: Input: list1 = [1,2,4], list2 = [1,3,4]Output: [1,1,2,3,4,4] Input: list1 = [], list2 = []Output: [] Input: list1 = [], list2 = [0]Output: [0] Constraints: The number of nodes in both lists is in the range [0, 50]. -100 ≤ Node.val ≤ 100 Both list1 and list2 are sorted in non-decreasing order. Approach:…
Problem Statement: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each node contains a single digit. Add the two numbers and return the result as a linked list. You may assume the two numbers do not contain any leading zeros, except the number 0 itself. Examples: Input: l1 = [2,4,3], l2 = [5,6,4]Output: [7,0,8]Explanation: 342 + 465 = 807 Input: l1 = [0], l2 = [0]Output: [0] Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]Output: [8,9,9,9,0,0,0,1] Constraints: The number of nodes in each linked list is in the range [1,…
Problem Statement: 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 (1st position), the second is even (2nd position), and so on. Example: Input: head = [1,2,3,4,5]Output: [1,3,5,2,4] Input: head = [2,1,3,5,6,4,7]Output: [2,3,6,7,1,5,4] Constraints: The number of nodes in the linked list is in the range [0, 104]. -106 ≤ Node.val ≤ 106 Approach: Maintain two pointers: odd starting from the first node and even from the second. Also store a reference to evenHead so…
Problem Statement: Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. Example: Input: head = [1,1,2]Output: [1,2] Input: head = [1,1,2,3,3]Output: [1,2,3] Constraints: The number of nodes in the list is in the range [0, 300]. -100 ≤ Node.val ≤ 100 The list is guaranteed to be sorted in ascending order. Approach: Iterate through the linked list with a current pointer. For each node, compare its value with the next node. If they are equal, skip the next node using curr.next = curr.next.next. Otherwise,…
Problem Statement: Given the head of a linked list, remove the nth node from the end of the list and return its head. Approach (Optimized Two-Pointer): Use a dummy node before the head to handle edge cases easily. Move the first pointer n steps ahead. Move both first and second pointers until first reaches the end. Now second is just before the node to be deleted. Skip it using second.next = second.next.next. Time and Space Complexity: Time Complexity: O(n) Space Complexity: O(1) JavaScript C C++ Java Python C# var removeNthFromEnd = function(head, n) { let sentinel = new ListNode(0, head);…
Problem Statement: Given the head of a linked list, remove the nth node from the end of the list and return its head. Examples: Input: head = [1,2,3,4,5], n = 2Output: [1,2,3,5] Input: head = [1], n = 1Output: [] Input: head = [1,2], n = 1Output: [1] Constraints: The number of nodes in the list is sz. 1 ≤ sz ≤ 30 0 ≤ Node.val ≤ 100 1 ≤ n ≤ sz Approach: Use a sentinel (dummy) node before the head to simplify edge cases. First, calculate the total length of the list. Find the previous node of the…
Contact Us
Subscribe to Stay Updated
Stay ahead in the world of tech with our exclusive newsletter! Subscribe now for regular updates on the latest trends, valuable coding resources, and tips to boost your frontend development skills.
