Tag: linkedlist
-
Find if a linked list is a palindrome or not
Problem: Given the head of a linked list , find if it is a palindrome or not. Find the solution in O(n) time and in place (O(1) space complexity) For example , the below linked list : 1 -> 2 -> 2 -> 1 is a palindrome The below is not: 1 -> 5 ->…
-
Add two numbers represented by Linked List
Given two numbers represented by a linked list in reverse order. You have two linked lists. Each of them represent a number which is not empty and not negative. Each of the node in the linked lists represent a single digit. The numbers are linked in reverse order. Create a linked list with their sum…
-
Data Structures & Algorithms in Java – Linked List – Merge k sorted lists.
Problem: Given k singly linked sorted lists merge them together into one sorted list. For example, given the below lists: [[1,4,5],[1,3,4],[2,6]] merge them into [1,1,2,3,4,4,5,6] Try out the solution here: https://leetcode.com/problems/merge-k-sorted-lists/ Solution: Hint: Let’s look at all of them . Brute force In this case , you copy all the node values into an array.…
-
Data Structures & Algorithms in Java – Linked List – Remove Nth Node from the last
Problem: Given a linked list , remove the nth node from the end of the list For example, For the linked list [1,2,3,4,5] and the number n = 3 Remove the 3rd element from the last which is 3. So the final list is [1,2,4,5] Try out the solution here: https://leetcode.com/problems/remove-nth-node-from-end-of-list/ Solution: Hint: Explanation: Two…
-
Data Structures & Algorithms in Java – Linked List – Reorder List
Problem: Given a singly linked list , reorder it in such a way that the first and last elements are next to each other , the second and second from last are next to each other etc. For example, Given the list [1,2,3,4,5] Reorder it into: [1,5,2,4,3] As you see we are merging elements from…
-
Data Structures & Algorithms in Java – Linked List – Merge Two Sorted Lists
Problem : Given two sorted lists merge them without copying them to a new linked list. For example: Given the linked lists: [1,3,4] and [1,2,4]. merge them into [1,1,2,3,4] Try out the solution here: https://leetcode.com/problems/merge-two-sorted-lists/submissions/ Solution: Hint: Explanation: The problem has a recursive solution and an iterative solution. Let’s look at both of them. In…
-
Data Structures & Algorithms in Java – Linked Lists- Linked List Cycle
Problem: Given a linked list and the head node of the linked list find if there is a loop / cycle in the linked list, that is if the tail node points back to any of the previous nodes. For example: The below linked list has a cycle: Output is true for the above linked…
-
Data Structures & Algorithms in Java – Linked List – Reverse Linked List
Problem: Given the head of a singly linked list , reverse the list and return the new head of the reversed list. For example : [1,2,3,4,5] – the array represents the node with 1 as the head node Output should be [5,4,3,2,1] Visual representation: Try out the solution here: https://leetcode.com/problems/reverse-linked-list/ Solution: Hint: Make the second…