Split Linked List into two halves fractionally given n

Problem Given a linked list with head pointer and a number n. Split the linked list into two halves such that first half contains 1/n elements and remaining linkedlist contains remaining elements. Return the head of the second list or the right partition pointer. Examples Example 1: Input: head = [1, 2, 3, 4, 5, 6, 7, 8], n = 2 Output: [5, 6, 7, 8] Explanation: As n = 2, we have 2 lists like this [ [1, 2, 3, 4], [5, 6, 7, 8] ] and we return the list with head at 5. ...

Reverse Linked List Problem

Problem Reverse a linked list. Follow up 1 - Do it in-place and in one-pass. Follow up 2 - Can you do it with only 2 pointers. Examples For example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL ...

Remove Nth Node From End of List

Problem Given a linked list, remove the nth node from the end of list and return its head. Examples Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5] Explanation: Given linked list 1->2->3->4->5 and n = 2, the result is 1->2->3->5. ...

Add Two Numbers represented as Linked List in Reversed Order

Problem You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Examples Example 1: (2 -> 4 -> 3) + (5 -> 6 -> 4) => 7 -> 0 -> 8 Input: l1 = [2,4,3], l2 = [5,6,4] Output: [7,0,8] Explanation: 342 + 465 = 807 ...

Remove duplicates from Sorted List 2

Problem Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well. Examples Example 1: Input: head = [1,2,3,3,4,4,5] Output: [1,2,5] ...

Remove Linked List Elements Problem

Problem Remove all elements from a linked list of integers that have value val. Example Example 1: Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 Return: 1 --> 2 --> 3 --> 4 --> 5 ...

Palindrome Linked List Problem

Problem Given the head of a singly linked list, return true if it is a palindrome or false otherwise. Examples Example 1: Input: head = [1,2,2,1] Output: true head = 1 -> 2 -> 2 -> 1 Example 2: Input: head = [1,2] Output: true head = 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 ...

Split Linked List 1 - At Middle

Problem Given a list, split it into two sublists — one for the front half, and one for the back half. If the number of elements is odd, the extra element should go in the front list. Examples Example 1: Input: head = [1,2,3,4,5 Output: [ [1,2,3], [4,5] ] ...

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