Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Example: Input: [   1->4->5,   1->3->4,   2->6 ] Output: 1->1->2->3->4->4->5->6 Solution Java Program Implementation /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { …

Continue reading Merge k Sorted Lists

13) Remove Nth Node From End of List

Hi friends, in this article we going to see how to remove the Nth node from end of the list. Here is our given problem, Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node …

Continue reading 13) Remove Nth Node From End of List

12) Linked List – Add Two Numbers

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 contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example: Input: (2 -> …

Continue reading 12) Linked List – Add Two Numbers

11) Palindrome Linked List

This is our given problem, Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false Example 2: Input: 1->2->2->1 Output: true Follow up:Could you do it in O(n) time and O(1) space? How can we APPROACH this problem? By using two ways, we can solve this problem, Slow …

Continue reading 11) Palindrome Linked List

10) Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 Explanation L1 = 1->4 L2 = 2->3 Output = 1->2->3->4 Create a dummy node. ListNode dummy = new ListNode(0); //dummy = …

Continue reading 10) Merge Two Sorted Lists

9) Linked List Cycle II

In this article we going to see about how to Detect Loop in a Linked List and Return the Loop Node. Below is our given problem from Leetcode, Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given linked list, we …

Continue reading 9) Linked List Cycle II

8) Detect Loop in linked list

In this article, we going to see how to detect loop in a linked list. Here is our given problem, Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects …

Continue reading 8) Detect Loop in linked list

7) Reverse a Linked List – Iterative and Recursive approach

This is one of the most important interview question, How to reverse a linked list using iterative and recursive approach? In this article, I will help you to understand, how to approach and solve this problem. I'm going to make a diagrammatic representations, so that you can easily understand the problem well. Here is our …

Continue reading 7) Reverse a Linked List – Iterative and Recursive approach

6) Swap Nodes in Pairs

Hi geeks! Today we going to see about how to swap the linked list nodes in a pair wise. Given a linked list, swap every two adjacent nodes and return its head. You may not modify the values in the list's nodes, only nodes itself may be changed. Example: Given 1->2->3->4, you should return the list as 2->1->4->3. …

Continue reading 6) Swap Nodes in Pairs

5) Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Example 1: Input: 1->2->3->3->4->4->5 Output: 1->2->5 Example 2: Input: 1->1->1->2->3 Output: 2->3 Solution: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; …

Continue reading 5) Remove Duplicates from Sorted List II

4) Remove Zero Sum Consecutive Nodes from Linked List

Hi geeks! In this article, we going to play with Linked list and hash map by solving this interesting problem remove Zero Sum Consecutive Nodes from Linked List. Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences. After doing so, return the head of …

Continue reading 4) Remove Zero Sum Consecutive Nodes from Linked List

3) Next Greater Node In Linked List

Hi friends! In this article, we going to find next greater node in linked list after that we going to convert that into an array. Lets jump into problem. We are given a linked list with head as the first node.  Let's number the nodes in the list: node_1, node_2, node_3, ... etc. Each node may have a next larger value: for node_i, next_larger(node_i) is …

Continue reading 3) Next Greater Node In Linked List

2) Rotate List

Hi Geeks! Today we going to see, how to rotate the linked list with fun. Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2->3->4->NULL rotate 2 steps to the right: 4->5->1->2->3->NULL Example 2: Input: 0->1->2->NULL, …

Continue reading 2) Rotate List

1) Design Linked List

Hi geeks, in this article we going to design the Linked List with add at head, add at tail, insert at specific position and delete operations. This is one of the good question from Leetcode to test the LinkedList knowledge. Design your implementation of the linked list. You can choose to use the singly linked list …

Continue reading 1) Design Linked List

Remove Last Node from the List

Given a Linked List, we need to remove the last node or last element from the List. Input : 1 -> 2 -> 3 -> 4 -> 5 Output : 1 -> 2 -> 3 -> 4 Straight Forward Approach 1)Check the edge cases Checking the edge cases is crucial in programming world. It is …

Continue reading Remove Last Node from the List