Linked List: Program to Check if a Singly Linked List is Palindrome

Problem Statement: You are given a linked list, you need to check if it is a palindrome or not Example B -> A -> C -> A -> B True Solution We can solve this problem using different methods. We shall see 2 methods: Method 1: Using Stacks. Method 2: By reversing the list. Method […]

Linked List: Find middle element in Linked List

Problem Statement: You are given an single LL. You need to fing the middle element. Example Input : 1->2->3->4->5->6 Output : 4 Solution The solution is very simple. We take 2 pointers, Fast and Slow. Fast will move 2 places at a time. Slow will move 1 place at a time. When fast pointer reaches […]

Linked List: Add 1 to the linked list

Problem Statement: You are given an linked list, you need to add one to it and return as result. Example Example 1: Input: 1 -> 9 -> 9 Output: 2 -> 0 -> 0 Input: 9 -> 9 -> 9 Output: 1 -> 0 -> 0 -> 0 Solution Before we solve this problem, there […]

Linked List: Move last element to front of a given Linked List

Problem Statement: You are given an LL, you need to move the last element to the first. Example Input: 1->2->3->4->5 Output: 5->1->2->3->4. Solution The solution is very simple. Traverse till the last node. Then perform below operations: 1. Make the second last as last 2. Set the last node as head. 3. Make it as […]

Linked List: Remove duplicates from an unsorted linked list

Problem Statement: You are given an un-sorted Linked List. You need to remove the duplicate nodes. Example Input: 2 -> 3 -> 3 -> 1 -> 2 -> 4 -> 4 -> 5 Output: 2 -> 3 -> 1 -> 4 -> 5 Solution In this solution, we will use 2 loops. For every element […]

Linked List: Reverse Linked List in groups

Problem Statement: You are given a linked list and an integer k, you need to reverse it group wise. Example Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> NULL k =3 Output: 3 -> 2 -> 1 -> 6 -> 5 -> 4 -> 8 -> […]

Searching and Sorting: Factorial Trailing Zeroes

Problem Statement: you are given an integer, you need to return the number of trailing zeros in n!. Example Input: n = 5 Output: 1 Because the factorial of 5 is 120 which has one trailing 0. Solution We know that, to get a trailing zeros, we need to check what will generate a trailing […]

Searching and Sorting: Find the missing number in Arithmetic Progression

Problem Statement: You are given an array that has elements of arithmetic progression in order. One element is missing, you need to find that Example Input: arr[] = {2, 4, 6, 10, 12, 14} Output: 8 Solution This problem can be solved in number of different methods. Method 1: You can traverse the array linearly […]

Searching and Sorting: K-th Element of Two Sorted Arrays

Problem Statement: You are given 2 arrays that are sorted. You need to find the k’th element if the two arrays are sorted and merged. Example Array 1 – 1 3 5 Array 2 – 2 4 6 k = 3 Sorted array = {1, 2, 3, 4, 5, 6} The 3rd element of the […]