{"id":18493256,"url":"https://github.com/aswinbarath/linked-lists","last_synced_at":"2025-04-08T22:30:43.139Z","repository":{"id":117712519,"uuid":"384661621","full_name":"AswinBarath/Linked-lists","owner":"AswinBarath","description":"Problems based on the Linked list data structure and various patterns","archived":false,"fork":false,"pushed_at":"2024-03-04T19:33:56.000Z","size":9605,"stargazers_count":5,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-23T18:51:57.215Z","etag":null,"topics":["java","linked-list","problem-solving"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AswinBarath.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2021-07-10T09:36:31.000Z","updated_at":"2024-12-21T21:31:25.000Z","dependencies_parsed_at":null,"dependency_job_id":"fcba828f-09d8-460f-98f8-00b4b1327aec","html_url":"https://github.com/AswinBarath/Linked-lists","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AswinBarath%2FLinked-lists","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AswinBarath%2FLinked-lists/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AswinBarath%2FLinked-lists/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AswinBarath%2FLinked-lists/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AswinBarath","download_url":"https://codeload.github.com/AswinBarath/Linked-lists/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247939788,"owners_count":21021845,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","linked-list","problem-solving"],"created_at":"2024-11-06T13:13:24.335Z","updated_at":"2025-04-08T22:30:38.127Z","avatar_url":"https://github.com/AswinBarath.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Linked lists\r\n\r\n\u003cp\u003e\u003cimg src=\"https://upload.wikimedia.org/wikipedia/commons/6/6d/Singly-linked-list.svg\"\u003e\u003c/p\u003e\r\n\r\nProblems based on the Linked list data structure\r\n\r\n\u003chr /\u003e\r\n\r\n## SDE Sheet problems on Linked lists\r\n\r\n[Sheet Link](https://takeuforward.org/interviews/strivers-sde-sheet-top-coding-interview-problems/)\r\n\r\n### Day 5\r\n\r\n| Completion Status | Linked List Problem | Explanation | Solution |\r\n| --- | --- | --- | --- |\r\n| ✅ | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Iterative Approach](#reverse-a-linkedlist) | [Java Soultion](./src/sde_sheet/ReverseLL.java) |\r\n| ✅ | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Brute - Optimal Approach](#find-the-middle-of-linkedList-amazon-microsoft) | [Java Soultion](./src/sde_sheet/MiddleOfLL.java) |\r\n| ✅ | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [Brute force Approach](#merge-two-sorted-linked-list) | [Java Soultion](./src/sde_sheet/MergeTwoSortedLists.java) |\r\n| ✅ | [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/submissions/) | [Iterative Approach]() | [Java Soultion](./src/sde_sheet/RemoveNthNodeFromEndofList.java)\r\n| ✅ | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Iterative Approach]() | [Java Soultion](./src/sde_sheet/DeleteNodeinaLinkedList.java) |\r\n| ✅ | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [Iterative Approach]() | [Java Soultion](./src/sde_sheet/AddTwoNumbers.java) |\r\n\r\n---\r\n\r\n### Day 6\r\n\r\n| Completion Status | Linked List Problem | Explanation | Solution |\r\n| --- | --- | --- | --- |\r\n| ✅ | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | [Brute - Better - Optimal I \u0026 II Approach](#intersection-of-two-linked-lists) | [Java Soultion](./src/sde_sheet/InterectionOfTwoLL.java) |\r\n| ✅ | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) | [Brute \u0026 Optimal Approach](#linked-list-cycle) | [Java Soultion](./src/sde_sheet/LinkedListCycle.java) |\r\n| ✅ | [Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/) | [Optimal Approach](#reverse-nodes-in-k-group) | [Java Soultion](./src/sde_sheet/ReverseKGroup.java) |\r\n| ✅ | [Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/) | [Iterative Approach]() | [Java Soultion](./src/sde_sheet/PalindromeLL.java) |\r\n| ✅ | [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) | [Brute \u0026 Optimal Approach](#find-the-starting-point-of-the-cycle) | [Java Soultion](./src/sde_sheet/FindStartingPointInLLloop.java) |\r\n| ✅ | [Flattening a Linked List](https://practice.geeksforgeeks.org/problems/flattening-a-linked-list/1#) | [Iterative Approach]() | [Java Soultion]() |\r\n| ✅ | [Rotate List](https://leetcode.com/problems/rotate-list/) | [Iterative Approach](#) | [Java Soultion](./src/sde_sheet/RotateLL.java) |\r\n\r\n\r\n---\r\n\r\n## Linked List Problems Rundown \r\n### (Approaches to Solve)\r\n\r\n### Reverse a LinkedList \r\n\r\n- Create a dummy node - **newHead** and point it to null.\r\n- Run a loop until the head of LL reaches null.\r\n\t- For each iteration, create a node **next** which stores the next reference of head.\r\n\t- Break the next reference of the head by pointing it towards the **newHead**.\r\n\t- Reassign **newHead** with head and then head with node **next**.\r\n- Return the **newHead**\r\n- Time Complexity: O(N) | Space Complexity: O(1)\r\n\r\n---\r\n\r\n### Find the middle of LinkedList \r\n#### `Amazon | Microsoft`\r\n\r\n#### Brute-force\r\n- Count all nodes in the given Linked list in one iteration\r\n- Now, calculate mid as the count by two and add one only if the size is even\r\n- Traverse again to reach the mid and return the mid node\r\n- Time Complexity: O(N) + O(N/2) | Space Complexity: O(1) \r\n\r\n#### Optimal: Runner Technique \r\n- Take two nodes slow and fast and point them to the head node.\r\n- Move the slow node by a distance of one and the fast node by a distance of two.\r\n- When the fast node completes the traversal, the slow node has reached the middle.\r\n- Return the slow node.\r\n- Time Complexity: O(N/2) | Space Complexity: O(1)\r\n\r\n---\r\n\r\n### Merge two sorted Linked List \r\n#### `Yahoo | Amazon`\r\n\r\n#### Brute-force \r\n#### Create new LL\r\n\r\n- Take the head nodes of the given two linked lists as h1 and h2\r\n- Create a dummy node d initialized to null\r\n- Compare the node values of h1 and h2 and take the smaller one\r\n- Create a new linked list and insert the smaller value into the LL.\r\n- Now, point next of the dummy node to the LL, and create another duplicate dummy node dd to create new nodes\r\n- If both of the heads h1 and h2 reach null, point the dd node to null as well.\r\n- Repeat the comparisons and return dummy node d as the new head of LL.\r\n- Time Complexity: O(n1 + n2) | Space Complexity: O(n1 + n2) \r\n\r\n#### Optimal approach\r\n#### In-place splicing of LLs\r\n\r\n- Take two dummy nodes l1 and l2 pointing to larger and smaller LLs respectively\r\n- Point l1 to whichever node value is smaller among given LLs and point l2 to larger node value\r\n- Take another dummy node res which points to l1. This res is the resultant answer by the end of function execution\r\n- Take a dummy node temp to store the smaller value among LL ( Basically to remember the last smallest node )\r\n- Traverse through the LLs until:\r\n\t- l1 is smaller than l2\r\n\t- Reassign the value to temp\r\n\t- When l1 becomes greater than l2:\r\n\t\t- Break the next of temp\r\n\t\t- Point the next of temp to l2\r\n\t\t- Swap l1 and l2 ( Because l1 got bigger than l2)\r\n\t\t- Change temp to null\r\n\t- Repeat this iteration until l1 becomes null\r\n- Time Complexity: O(n1 + n2) | Space Complexity: O(1)\r\n\r\n\u003cp\u003e\r\n\t\u003cimg src=\"./Assets/MergeSortedLLinPlaceDryRun.png\" width=\"600px\"/\u003e\r\n\u003c/p\u003e\r\n\r\n---\r\n\r\n\r\n### Intersection of Two Linked Lists \r\n#### `Amazon | Microsoft`\r\n\r\n#### Brute Force approach\r\n\r\n- Check for every node in second LL to check each node in first LL for equality\r\n- When you found equal nodes return it, else you will reach the end hence return null\r\n- Time Complexity: O(m * n); where m and n are length of LL 1 \u0026 2 respectively\r\n\r\n#### Better approach \r\n#### Hashing\r\n\r\n- Traverse through first LL and hash the **Node Address**\r\n- Again traverse for second LL and check for equality\r\n- When you found equal node address from HashMap return it, else you will reach the end hence return null\r\n- Time Complexity: O(m + n); where m and n are length of LL 1 \u0026 2 respectively\r\n- Space Complexity: O(m); where m and n are length of LL 1 \u0026 2 respectively\r\n\r\n#### Optimal approach I - ( Lengthier approach )\r\n\r\n- Take dummy nodes at head of both LL, and keep a count to store length of both the LL\r\n- Take dummy nodes at head of both LL again, and cover the difference one LL using lengths\r\n- Then, traverse simultaneously both the nodes to reach an intersection point\r\n- When you found equal nodes return it, else you will reach the end hence return null\r\n- Time Complexity = O(m) \u003c= O(2m) \u003c= { O(m) + O(m-n) + O(n) }; where m is length of longer LL and n is length of shorter LL\r\n\r\n#### Optimal approach II - ( Concise approach )\r\n\r\n- Take two dummy nodes d1 and d2, assign them to head of the LL\r\n- Move d1 and d2 simultaneously till one of them reaches null\r\n- Now, reassign that dummy node that is at null to the head of the other LL\r\n- Repeat the same process for both nodes until them meet at intersection\r\n- When you found equal nodes return it, else you will reach the end hence return null\r\n\r\n\r\n#### Intuition behind Optimal approach II\r\n\r\n- The first iteration is to find the difference directly and reassigning them to equate the difference\r\n- Hence in the second iteration they either meet at intersection if exists or null\r\n- This approach is similar Optimal approach I, but instead of calculating difference, here we directly find it with our logic\r\n- Time Complexity: O(2m); where m is the length of longer LL\r\n\r\n---\r\n\r\n### Linked List Cycle \r\n#### `Amazon | Samsung | Microsoft`\r\n\r\n#### Brute force approach \r\n#### Hashing\r\n\r\n- Take a dummy node d and traverse through the Linked List\r\n- Create a HashMap to store Nodes and check whether the nodes exist already\r\n- Hash the node itself if it's not present in the HashMap\r\n- Return the point where the node exists or when we reach null\r\n- Time Complexity: O(N) | Space Complexity: O(N)\r\n\r\n#### Optimal \r\n#### Runner Technique\r\n\r\n- Take two pointers slow and fast\r\n- Traverse slow pointer by one step and fast pointer by two steps\r\n- If cycle exists, we can be pretty sure slow and fast pointer meet again\r\n- If not then return null\r\n- Time Complexity: O(N) | Space Complexity: O(1)\r\n\r\n#### Intuition for Runner Technique\r\n\r\n- As the fast pointer moves by two steps, ultimately it will meet at any one point\r\n\r\n---\r\n\r\n### Reverse Nodes in k-Group \r\n#### `Hard Problem from LeetCode`\r\n\r\n- Take four dummy nodes\r\n- One to point to the new head of LL\r\n- Other three to keep track of previous node, current node and the next node\r\n- Apply reversing LL technique for k groups and keep on updating the dummy nodes\r\n- Return the new head\r\n- Time Complexity: O(N) | Space Complexity: O(1)\r\n\r\n**Dry Run**\r\n\r\n\u003cp\u003e\r\n\t\u003cimg src=\"./Assets/Reverse Nodes in k-Group.jpg\" width=\"600px\" \u003e\r\n\u003c/p\u003e\r\n\r\n---\r\n\r\n### Find the starting point of the cycle\r\n\r\n#### Brute force approach \r\n#### Hashing\r\n\r\n- Hash all the nodes in a HashMap\r\n- When the same node occurs twice return it, else return null\r\n- Time Complexity: O(N) | Space Complexity: O(N)\r\n\r\n#### Optimal approach \r\n#### Runner Technique\r\n\r\n- 1. Find the collision point\r\n- Move slow pointer by 1 step and fast pointer by 2 steps\r\n- They are bound to collide at one node if there exists a cycle\r\n- 2. Find the Starting point of LL Cycle\r\n- Take entry pointer from head of LL \r\n- Move both **entry** and **slow** pointers by 1 step\r\n- When entry and slow pointers collide, that's the starting point of the LL\r\n- Hence return that node \r\n- If no cycle exists, fast will reach null, hence return it\r\n- Time Complexity: O(N) | Space Complexity: O(1)\r\n\r\n#### Intuition behind this algorithm\r\n\r\n\u003cp\u003e\r\n\t\u003cimg src=\"./Assets/FindStartingPointLLcycleIntuition.png\" width=\"600px\"/\u003e\r\n\u003c/p\u003e\r\n\r\n---\r\n\r\n### Flattening a Linked List\r\n\r\n#### Amazon, Flipkart, Goldman Sachs, Microsoft, Paytm, Qualcomm, Snapdeal, Visa\r\n\r\n#### Optimal Approach\r\n#### Recursive approach + Merge Two Sorted LL \r\n\r\n- The problem states that we are given a LL with two pointers - next and bottom\r\n- We have to flatten given LL in such a way that the first node must contain all other nodes connected using bottom pointer in a sorted order\r\n- So, if we can try to sort each LL from the back, we can reach to the solution\r\n- We can sort two linked lists recursively using Merge Two Sorted LL logic\r\n- Time Complexity: O(N) | Space Complexity: O(1)\r\n\r\n**Dry Run**\r\n\r\n\u003cp\u003e\r\n\t\u003cimg src=\"./Assets/Flattening a Linked List.jpg\" width=\"600px\" \u003e\r\n\u003c/p\u003e\r\n\r\n---\r\n\r\n#### Rotate List\r\n\r\n#### Adobe, Amazon, Microsoft, Goldman Sachs, MMT\r\n\r\n#### Brute force approach\r\n\r\n- Rotate the LL by indivdually adding nodes from the end\r\n- Time Complexity: O(k * N) | Space Complexity: O(1)\r\n\r\n\r\n#### Optimal approach\r\n\r\n- Count the length of Linked list\r\n- Correct the value of k\r\n- Point last node to first node, once you are done with first step itself\r\n- Traverse for `len-k` times, and save the next node in temp\r\n- Then break the next's link and reassign it to null\r\n- Return the temp\r\n- Time Complexity: O(N) | Space Complexity: O(1)\r\n\r\n**Dry Run**\r\n\r\n\u003cp\u003e\r\n\t\u003cimg src=\"./Assets/Rotate a LL.jpg\" width=\"600px\" \u003e\r\n\u003c/p\u003e\r\n\r\n---\r\n\r\n### Clone a Linked List with next and random pointer\r\n\r\n#### Brute force approach \r\n#### Hashing\r\n\r\n- Hash the current node with new duplicate node for the first traversal\r\n- Assign next and random pointers, one by one, using Hashmap for the next traversal\r\n- Return the new clone (deep copy) of LL\r\n\r\n#### Optimal appraoch\r\n\r\n- Copy nodes and insert them right after the original nodes\r\n- In the next iteration, Assign random pointers for the copy nodes using a dummy node `iter` and original nodes\r\n- Restore the original list, and extract the copy list\r\n\r\n---\r\n\r\n## Task list\r\n\r\n- [x] Introduction\r\n- [x] Linked Lists VS Arrays\r\n- [ ] Operations\r\n- [ ] Implementation:\r\n\t- [x] Singly Linked List\r\n\t- [ ] Doubly Linked List\r\n\t- [ ] Circular Linked List\r\n- [ ] Collections Framework\r\n\t- [ ] List Interface\r\n\t- [ ] LinkedList Class\r\n- [ ] Time and Space Complexity\r\n- [ ] Fast and slow pointer\r\n- [ ] Cycle Detection\r\n- [ ] Reversal of LinkedList\r\n- [ ] Problems\r\n\t- [x] [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)\r\n\t- [x] [Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/)\r\n\t- [x] [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/)\r\n\t- [x] [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/)\r\n\t- [x] [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/)\r\n\t- [x] [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/submissions/)\r\n\t- [x] [Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/)\r\n\t- [ ] [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/)\r\n\t- [x] [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/)\r\n\t- [x] [Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/)\r\n\t- [x] [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/)\r\n\t- [x] [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/)\r\n\t- [x] [Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/)\r\n\t- [x] [Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/)\r\n\t- [x] [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/)\r\n\t- [ ] [Flattening a Linked List](https://practice.geeksforgeeks.org/problems/flattening-a-linked-list/1#)\r\n\t- [ ] [Rotate List](https://leetcode.com/problems/rotate-list/) \r\n\t- [ ] [Implement Stack using Linked List](https://practice.geeksforgeeks.org/problems/implement-stack-using-linked-list/1)\r\n\t- [ ] [Implement Queue using Linked List](https://practice.geeksforgeeks.org/problems/implement-queue-using-linked-list/1)\r\n\t- [ ] [LRU Cache](https://leetcode.com/problems/lru-cache/)\r\n\t- [ ] [Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/)\r\n\t- [ ] Segregate Even Odd\r\n\t- [ ] Union and Intersection\r\n\t- [ ] Detect and Remove cycle\r\n\t- [ ] Clone LinkedList\r\n\r\n\u003chr /\u003e\r\n\r\n## Linked Lists Definition\r\n\r\n- Linked List are linear data structures where every element is a separate object with a data part and address part. \r\n- Each object is called a Node. \r\n- The nodes are not stored in contiguous locations. They are linked using addresses.\r\n- In a linkedlist we can create as many nodes as we want according to our requirement. This can be done at the runtime. \r\n- The memory allocation done by the JVM at runtime is known as DYNAMIC MEMORY ALLOCATION. \r\n- Thus, linked list uses dynamic memory allocation to allocate memory to the nodes at the runtime.\r\n\r\n\u003chr /\u003e\r\n\r\n\r\n## Linked Lists VS Arrays\r\n\r\n| Linked Lists                              | Arrays                                |\r\n| ------------                              | -----------                           |\r\n| Linked lists can grow dynamically         | Array cannot grow Dynamically         |\r\n| Sequential access of elements is possible | Random access of elements is possible |\r\n\r\n\r\n---\r\n\r\n\r\n## Linked Lists Patterns\r\n\r\n### Reverse Linked List Pattern\r\n\r\n- [x] [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)\r\n- [x] [Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/)\r\n\r\n\r\n### Runner Pattern (Also called Tortoise-Hare Technique)\r\n\r\n- [x] [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/)\r\n- [x] [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/submissions/) `Amazon`, `Microsoft`, `Adobe`\r\n\r\n\r\n---\r\n\r\n## Tutorials\r\n\r\n| Linked List - Singly + Doubly + Circular (Theory + Code + Implementation | Linked List Interview Questions - Google, FAANGM |\r\n| - | - |\r\n| [![Linked List - Singly + Doubly + Circular (Theory + Code + Implementation)](https://img.youtube.com/vi/58YbpRDc4yw/0.jpg)](https://www.youtube.com/watch?v=58YbpRDc4yw) | [![Linked List Interview Questions - Google, Facebook, Amazon, Microsoft, Apple, Twitter, LinkedIn](https://img.youtube.com/vi/70tx7KcMROc/0.jpg)](https://www.youtube.com/watch?v=70tx7KcMROc) |\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faswinbarath%2Flinked-lists","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faswinbarath%2Flinked-lists","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faswinbarath%2Flinked-lists/lists"}