question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
movement-of-robots | C++ video | c-video-by-mwca2024-vw8h | \nusing ll = long long;\nclass Solution {\npublic:\n int sumDistance(vector& nums, string s, int d) {\n int mod = 1e9+7;\n vector pos;\n | mwca2024 | NORMAL | 2023-06-11T09:38:26.348024+00:00 | 2023-06-11T09:38:26.348072+00:00 | 13 | false | \nusing ll = long long;\nclass Solution {\npublic:\n int sumDistance(vector<int>& nums, string s, int d) {\n int mod = 1e9+7;\n vector<int> pos;\n \n int n = nums.size();\n for(int i = 0; i < n; ++i) {\n if(s[i] == \'L\') pos.push_back(nums[i] - d);\n else pos... | 0 | 0 | ['C++'] | 0 |
movement-of-robots | EASY C++ solution using sorting and prefix sum | easy-c-solution-using-sorting-and-prefix-i84t | \n\n# Complexity\n- Time complexity:\nO(nlogn)\n\n- Space complexity:\nO(1sorting)\n\n# Code\n\nclass Solution {\npublic:\n int sumDistance(vector<int>& nums | levii_31 | NORMAL | 2023-06-11T09:26:12.606816+00:00 | 2023-06-11T09:28:03.527587+00:00 | 50 | false | \n\n# Complexity\n- Time complexity:\nO(nlogn)\n\n- Space complexity:\nO(1sorting)\n\n# Code\n```\nclass Solution {\npublic:\n int sumDistance(vector<int>& nums, string s, int d) {\n int n=nums.size();\n long long ans=0,sum=0;\n int mod=1e9+7;\n //as we need only final position of robots ... | 0 | 0 | ['Math', 'Sorting', 'Prefix Sum', 'C++'] | 0 |
movement-of-robots | 🦅 Python3 🎃 Clear Code with Intuition and Brainstorm Procedure | python3-clear-code-with-intuition-and-br-18hr | Intuition\nI can\'t solve this problem until realize the first difficulty, changing direction, it is a honey pot to decrease our focus.\nAnd second difficulty i | liuliugit | NORMAL | 2023-06-11T08:48:17.071825+00:00 | 2023-06-11T08:48:17.071871+00:00 | 25 | false | # Intuition\nI can\'t solve this problem until realize the first difficulty, changing direction, it is a honey pot to decrease our focus.\nAnd second difficulty is how to get distances sum in sufficient time complexity.\n\n# Approach\nHow to Solve Second Difficulty:\n> When nums is unordered list, we need to use abs() ... | 0 | 0 | ['Python3'] | 0 |
flatten-a-multilevel-doubly-linked-list | Easy Understanding Java beat 95.7% with Explanation | easy-understanding-java-beat-957-with-ex-d34s | Basic idea is straight forward: \n1. Start form the head , move one step each time to the next node\n2. When meet with a node with child, say node p, follow its | kyleluchina | NORMAL | 2018-07-16T04:09:47.449252+00:00 | 2018-10-23T21:52:38.704662+00:00 | 50,425 | false | Basic idea is straight forward: \n1. Start form the `head` , move one step each time to the next node\n2. When meet with a node with child, say node `p`, follow its `child chain` to the end and connect the tail node with `p.next`, by doing this we merged the `child chain` back to the `main thread`\n3. Return to `p` an... | 554 | 8 | [] | 65 |
flatten-a-multilevel-doubly-linked-list | c++, about 10 lines solution | c-about-10-lines-solution-by-zjzh-sxls | \nNode* flatten(Node* head) {\n\tfor (Node* h = head; h; h = h->next)\n\t{\n\t\tif (h->child)\n\t\t{\n\t\t\tNode* next = h->next;\n\t\t\th->next = h->child;\n\t | zjzh | NORMAL | 2018-07-21T12:31:55.366483+00:00 | 2018-10-23T05:29:12.651725+00:00 | 19,832 | false | ```\nNode* flatten(Node* head) {\n\tfor (Node* h = head; h; h = h->next)\n\t{\n\t\tif (h->child)\n\t\t{\n\t\t\tNode* next = h->next;\n\t\t\th->next = h->child;\n\t\t\th->next->prev = h;\n\t\t\th->child = NULL;\n\t\t\tNode* p = h->next;\n\t\t\twhile (p->next) p = p->next;\n\t\t\tp->next = next;\n\t\t\tif (next) next->pr... | 329 | 3 | [] | 30 |
flatten-a-multilevel-doubly-linked-list | [C++] Simple 5 line recursive solution (with diagram) | c-simple-5-line-recursive-solution-with-cd75q | C++\nNode* flatten(Node* head, Node* rest = nullptr) {\n if (!head) return rest;\n head->next = flatten(head->child, flatten(head->next, rest));\n if (head-> | mhelvens | NORMAL | 2019-05-20T13:12:38.135853+00:00 | 2019-07-21T15:17:06.257832+00:00 | 11,372 | false | ```C++\nNode* flatten(Node* head, Node* rest = nullptr) {\n if (!head) return rest;\n head->next = flatten(head->child, flatten(head->next, rest));\n if (head->next) head->next->prev = head;\n head->child = nullptr;\n return head;\n}\n```\n\nThis function modifies the structure in place. It\'s not the fastest impl... | 226 | 1 | ['Recursion', 'C'] | 20 |
flatten-a-multilevel-doubly-linked-list | Python easy solution using stack | python-easy-solution-using-stack-by-shri-91ld | \nclass Solution(object):\n def flatten(self, head):\n if not head:\n return\n \n dummy = Node(0,None,head,None) \n | shrivilb | NORMAL | 2018-07-29T23:29:31.063338+00:00 | 2018-09-18T19:03:55.605316+00:00 | 16,423 | false | ```\nclass Solution(object):\n def flatten(self, head):\n if not head:\n return\n \n dummy = Node(0,None,head,None) \n stack = []\n stack.append(head)\n prev = dummy\n \n while stack:\n root = stack.pop()\n\n root.prev = pre... | 154 | 0 | [] | 23 |
flatten-a-multilevel-doubly-linked-list | Java Recursive Solution | java-recursive-solution-by-zhiying_qian-3l1z | \n public Node flatten(Node head) {\n \tflattentail(head);\n \treturn head;\n }\n\n // flattentail: flatten the node "head" and return the tail i | zhiying_qian | NORMAL | 2018-08-07T14:56:56.990428+00:00 | 2018-10-23T15:43:38.044318+00:00 | 17,169 | false | ```\n public Node flatten(Node head) {\n \tflattentail(head);\n \treturn head;\n }\n\n // flattentail: flatten the node "head" and return the tail in its child (if exists)\n // the tail means the last node after flattening "head"\n\n // Five situations:\n // 1. null - no need to flatten, just re... | 124 | 1 | [] | 15 |
flatten-a-multilevel-doubly-linked-list | [Python] DFS with stack, 2 solutions, exaplained | python-dfs-with-stack-2-solutions-exapla-lyrr | In this problem we need to traverse our multilevel doubly linked list in some special order and rebuild some connections. We can consider our list as graph, whi | dbabichev | NORMAL | 2020-07-10T07:35:41.807325+00:00 | 2020-07-10T08:58:38.218844+00:00 | 6,049 | false | In this problem we need to traverse our multilevel doubly linked list in some special order and rebuild some connections. We can consider our list as graph, which we now need to traverse. What graph traversal algorighms do we know? We should think about dfs and bfs. Why I choose dfs? Because we need to traverse as deep... | 105 | 6 | ['Depth-First Search'] | 8 |
flatten-a-multilevel-doubly-linked-list | C++ Super Simple, Short and Clean Iterative Solution, 4ms O(n) TC O(1) SC | c-super-simple-short-and-clean-iterative-z1ml | \nclass Solution {\npublic:\n Node* flatten(Node* head) {\n Node *ptr = head, *tmp_next, *runner;\n \n while (ptr) {\n if (pt | yehudisk | NORMAL | 2021-10-31T08:41:51.842736+00:00 | 2021-10-31T08:41:51.842767+00:00 | 4,296 | false | ```\nclass Solution {\npublic:\n Node* flatten(Node* head) {\n Node *ptr = head, *tmp_next, *runner;\n \n while (ptr) {\n if (ptr->child) {\n \n // Save the current next and connect the child to next\n tmp_next = ptr->next;\n ... | 82 | 1 | ['C'] | 7 |
flatten-a-multilevel-doubly-linked-list | Python solution with explanation | python-solution-with-explanation-by-snit-x3jb | The Idea\n\t\nWe will process the list "depth first" (i.e. to the lowest child), redirecting parent nodes next pointers to their children, and saving the previo | snitkovskiy | NORMAL | 2018-07-23T01:15:41.905869+00:00 | 2018-10-17T03:03:13.313645+00:00 | 6,878 | false | ## The Idea\n\t\nWe will process the list "depth first" (i.e. to the lowest child), redirecting parent nodes next pointers to their children, and saving the previous values of their next pointers in a Stack to be reattached once the bottom-most non-parent node has no siblings. \n\n## The algorithm\n### Pseudocode\n1. I... | 56 | 0 | [] | 10 |
flatten-a-multilevel-doubly-linked-list | Easy Understand Java Recursive solution beat 100% with Explanation | easy-understand-java-recursive-solution-8aupc | The idea is simple. We always keep a pre global variable to keep track of the last node we visited and connect current node head with this pre node. So for each | jiangjennifer | NORMAL | 2018-10-21T17:17:42.623204+00:00 | 2018-10-24T23:15:04.765672+00:00 | 4,113 | false | The idea is simple. We always keep a `pre` global variable to keep track of the last node we visited and connect current node `head` with this `pre` node. So for each recursive call, we do \n- Connect last visited node with current node by letting `pre.next` point to current node `head` and current node\'s `prev` point... | 44 | 0 | [] | 7 |
flatten-a-multilevel-doubly-linked-list | Java solution using stack. readable | java-solution-using-stack-readable-by-co-cyr8 | \nclass Solution {\n public Node flatten(Node head) {\n Stack<Node> stack = new Stack<>();\n Node travel = head;\n while(travel != null | CoachIWantToLeetcode | NORMAL | 2018-08-01T04:27:59.334505+00:00 | 2018-09-20T21:27:25.975733+00:00 | 2,400 | false | ```\nclass Solution {\n public Node flatten(Node head) {\n Stack<Node> stack = new Stack<>();\n Node travel = head;\n while(travel != null || !stack.isEmpty()) {\n if(travel.child != null) {\n if(travel.next != null) stack.push(travel.next);\n travel.next... | 32 | 1 | [] | 4 |
flatten-a-multilevel-doubly-linked-list | Python O(n), O(1) easy solution with explanation | python-on-o1-easy-solution-with-explanat-pztm | Easy iterative approach without using extra memory.\nO(n) time complexity\nO(1) space complexity\n\n\n"""\n# Definition for a Node.\nclass Node(object):\n de | jiriVFX | NORMAL | 2021-05-09T14:51:42.999144+00:00 | 2021-05-09T15:00:23.219452+00:00 | 3,742 | false | Easy iterative approach without using extra memory.\nO(n) time complexity\nO(1) space complexity\n\n```\n"""\n# Definition for a Node.\nclass Node(object):\n def __init__(self, val, prev, next, child):\n self.val = val\n self.prev = prev\n self.next = next\n self.child = child\n"""\n\ncla... | 31 | 0 | ['Iterator', 'Python', 'Python3'] | 8 |
flatten-a-multilevel-doubly-linked-list | Simple Java solution without recursion faster than 100% and memory less than 99% | simple-java-solution-without-recursion-f-atli | \nclass Solution {\n public Node flatten(Node head) {\n Node curr=head;\n while(curr!=null){\n if(curr.child != null){\n | neutrino10 | NORMAL | 2021-03-04T04:20:55.502110+00:00 | 2021-03-06T10:08:16.055083+00:00 | 2,338 | false | ```\nclass Solution {\n public Node flatten(Node head) {\n Node curr=head;\n while(curr!=null){\n if(curr.child != null){\n Node tail = findTail(curr.child);\n if(curr.next != null){\n curr.next.prev=tail;\n }\n \... | 30 | 0 | ['Java'] | 6 |
flatten-a-multilevel-doubly-linked-list | python recursive and iterative | python-recursive-and-iterative-by-maxwel-ofu1 | Recursive:\n\nclass Solution:\n def flatten(self, head: \'Node\') -> \'Node\':\n if not head: return None\n def travel(node):\n whil | maxwellnorah | NORMAL | 2019-04-06T20:47:09.119661+00:00 | 2019-04-06T20:47:09.119729+00:00 | 5,074 | false | Recursive:\n```\nclass Solution:\n def flatten(self, head: \'Node\') -> \'Node\':\n if not head: return None\n def travel(node):\n while node:\n q = node.next\n if not q: tail = node\n if node.child:\n node.next = node.child\n ... | 30 | 0 | ['Recursion', 'Iterator', 'Python'] | 4 |
flatten-a-multilevel-doubly-linked-list | 💡JavaScript Solution | javascript-solution-by-aminick-d0dt | The idea\n1. Use a stack to keep track of different levels of child nodes\n2. For each node, link back to the prev node. This way it\'s easier to link the end o | aminick | NORMAL | 2019-12-20T02:42:28.230962+00:00 | 2019-12-20T02:42:28.231011+00:00 | 2,050 | false | ### The idea\n1. Use a stack to keep track of different levels of child nodes\n2. For each node, link back to the `prev` node. This way it\'s easier to link the end of a child list to the parent list.\n``` javascript\nvar flatten = function(head) {\n if (!head) return null;\n let dummyHead = new Node(0, null, hea... | 27 | 0 | ['JavaScript'] | 0 |
flatten-a-multilevel-doubly-linked-list | [C++] Easy & Intuitive Recursive Solution | With Whiteboard Diagrams | No extra space | c-easy-intuitive-recursive-solution-with-530r | Explanation:\n\n\n\n\n/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* prev;\n Node* next;\n Node* child;\n};\n*/\n\nclass So | abhisharma404 | NORMAL | 2020-08-15T07:04:53.122730+00:00 | 2020-08-15T07:06:06.887947+00:00 | 2,416 | false | **Explanation:**\n\n\n\n```\n/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* prev;\n Node* next;\n Node* child;\n};\n*/\n\nclass Solution {\npublic:\n Node* flatten(Node* h... | 24 | 1 | ['Recursion', 'C', 'C++'] | 1 |
flatten-a-multilevel-doubly-linked-list | C++ | Two Solutions | c-two-solutions-by-ashwinfury1-fibc | Solution 1 - using array\n1. Iterate through nodes\n2. Add current node to array\n3. If the node has a child traverse the child\n4. At the end we will have an a | ashwinfury1 | NORMAL | 2020-07-10T08:37:29.602703+00:00 | 2020-07-10T08:48:47.980476+00:00 | 2,874 | false | ## Solution 1 - using array\n1. Iterate through nodes\n2. Add current node to array\n3. If the node has a child traverse the child\n4. At the end we will have an array in the order we want.\n5. Iterate through array and set next and prev pointer correctly also remove child nodes\n\nLet\'s take an example\n``` \n1---2-... | 23 | 2 | [] | 4 |
flatten-a-multilevel-doubly-linked-list | Java 1ms Recursion beats 100% with explaination | java-1ms-recursion-beats-100-with-explai-p88i | Go through the linked list;\nIf \n1. Node cur.child == null, skip, don\'t go into recursion.\n2. Node cur.child != null, recurse and get child\'s tail node. Nod | 351221254 | NORMAL | 2018-08-14T03:49:03.960524+00:00 | 2018-09-08T18:06:34.670198+00:00 | 2,471 | false | Go through the linked list;\nIf \n1. Node cur.child == null, skip, don\'t go into recursion.\n2. Node cur.child != null, recurse and get child\'s tail node. Node tmp = cur.next, (1)connect cur <-> child, (2)connect child\'s tail <->tmp.\n\nThe helper function returns the tail node of current level.\n\n```\npublic Node ... | 23 | 0 | [] | 1 |
flatten-a-multilevel-doubly-linked-list | 😎Brute Force to Efficient Method 100% beat🤩 | Java | C++ | brute-force-to-efficient-method-100-beat-grar | 1st Method :- Brute force\n>The brute-force approach involves using recursion to traverse the nested doubly linked list and flatten it. Here\'s how you can do i | Akhilesh21 | NORMAL | 2023-10-03T15:16:46.747722+00:00 | 2023-10-03T15:22:06.733628+00:00 | 2,044 | false | # 1st Method :- Brute force\n>The brute-force approach involves using recursion to traverse the nested doubly linked list and flatten it. Here\'s how you can do it step by step:\n\n1. Start at the head of the doubly linked list.\n2. Initialize a helper recursive function that takes a node as an argument.\n3. In the hel... | 22 | 1 | ['Stack', 'C++', 'Java'] | 4 |
flatten-a-multilevel-doubly-linked-list | javascript stack | javascript-stack-by-dolphinfight-6xz9 | \nvar flatten = function(head) {\n if (!head) return head;\n let stack = []; //store all rest part of linkedlist nodes when has child\n let cur = head; | dolphinfight | NORMAL | 2020-07-10T21:41:23.763102+00:00 | 2020-07-10T21:41:49.168113+00:00 | 991 | false | ```\nvar flatten = function(head) {\n if (!head) return head;\n let stack = []; //store all rest part of linkedlist nodes when has child\n let cur = head;\n while (cur){\n if (cur.child){\n if (cur.next) stack.push(cur.next); //must check cur.next is null or not before added to stack\n ... | 16 | 0 | ['JavaScript'] | 3 |
flatten-a-multilevel-doubly-linked-list | TC: O(n), SC: O(1) No stack, No recursion Simple One Pass Python Solution | tc-on-sc-o1-no-stack-no-recursion-simple-hdp0 | \ndef flatten(self, head: \'Optional[Node]\') -> \'Optional[Node]\':\n if not head: return\n curr = head\n while curr:\n if curr | azaansherani | NORMAL | 2022-04-14T20:42:21.711687+00:00 | 2022-06-12T13:03:35.663514+00:00 | 1,016 | false | ```\ndef flatten(self, head: \'Optional[Node]\') -> \'Optional[Node]\':\n if not head: return\n curr = head\n while curr:\n if curr.child:\n childNode = curr.child\n childNode.prev = curr #setting previous pointer\n \n whil... | 15 | 0 | ['Python', 'Python3'] | 4 |
flatten-a-multilevel-doubly-linked-list | Simple Java Pre-Order Solution | simple-java-pre-order-solution-by-wecanc-8rdj | \nclass Solution {\n public Node flatten(Node h) {\n if (h == null) return h;\n \n Stack<Node> st = new Stack<>();\n Node prev = | wecancode | NORMAL | 2018-12-28T06:59:04.596322+00:00 | 2018-12-28T06:59:04.596381+00:00 | 926 | false | ```\nclass Solution {\n public Node flatten(Node h) {\n if (h == null) return h;\n \n Stack<Node> st = new Stack<>();\n Node prev = null;\n st.push(h);\n while (!st.isEmpty()){\n Node cur = st.pop();\n if (prev != null) {\n prev.next = cu... | 14 | 0 | [] | 1 |
flatten-a-multilevel-doubly-linked-list | Java pre-order and post-order solution same idea with 114. Flatten Binary Tree to Linked List | java-pre-order-and-post-order-solution-s-7pqu | The idea is same as 114. Just imagine the attrs "child" and "next" in the Node as "left" and "right" in the TreeNode. You will find actually, this multi-level l | yunwei_qiu | NORMAL | 2019-05-30T23:27:42.764633+00:00 | 2019-05-30T23:28:29.431244+00:00 | 582 | false | The idea is same as 114. Just imagine the attrs "child" and "next" in the Node as "left" and "right" in the TreeNode. You will find actually, this multi-level linkedList is a tree but just add parent node as "prev". (Note: I met this problem when I was onsite interview with Bloomberg last year, that time I cannot solve... | 13 | 0 | [] | 4 |
flatten-a-multilevel-doubly-linked-list | [Python] Easy to read 1 pass iterative, while loop + stack + 3 if statements | python-easy-to-read-1-pass-iterative-whi-4ti7 | O(n) time and space complexity\n \n\n def flatten(self, head: \'Node\') -> \'Node\':\n if head:\n n_stack = [] \n curr_no | briantsui2018 | NORMAL | 2020-01-15T03:49:14.495374+00:00 | 2020-01-15T03:49:14.495417+00:00 | 725 | false | O(n) time and space complexity\n \n```\n def flatten(self, head: \'Node\') -> \'Node\':\n if head:\n n_stack = [] \n curr_node = head\n while curr_node:\n if curr_node.next:\t\t\t\t\t\t# Push the "next" (if there\'s a "next") first.\n n... | 11 | 0 | ['Iterator', 'Python'] | 0 |
flatten-a-multilevel-doubly-linked-list | Simple Java Iteration solution beats 100% with explanation | simple-java-iteration-solution-beats-100-8t2q | We can use a stack to store the next node when the current node has a child, so we can go back to it when we reach to the end of the current list. \n\n\n pub | jay_fyi | NORMAL | 2019-04-26T22:35:01.230311+00:00 | 2019-04-26T22:35:01.230376+00:00 | 1,663 | false | We can use a stack to store the next node when the current node has a child, so we can go back to it when we reach to the end of the current list. \n\n\n public Node flatten(Node head) {\n //We can use a stack to store the next node when the current node has a child, so we can go back to it when we reach to t... | 11 | 0 | ['Stack', 'Doubly-Linked List', 'Iterator', 'Java'] | 4 |
flatten-a-multilevel-doubly-linked-list | Easy Java Solution, 100% faster and takes 0ms and O(1) space | easy-java-solution-100-faster-and-takes-s09ko | \npublic Node flatten(Node head) {\n if(head==null) return head;\n Node curr = head;\n \n while(curr!=null){\n if(curr.ch | harshadan03 | NORMAL | 2020-07-10T12:05:47.326902+00:00 | 2020-07-10T12:05:47.326949+00:00 | 512 | false | ```\npublic Node flatten(Node head) {\n if(head==null) return head;\n Node curr = head;\n \n while(curr!=null){\n if(curr.child!=null){\n Node down = curr.child;\n while(down.next!=null) down = down.next;\n Node temp = curr.next;\n ... | 10 | 1 | [] | 3 |
flatten-a-multilevel-doubly-linked-list | The linked list [x,y,z] is not a valid doubly linked list error?? | the-linked-list-xyz-is-not-a-valid-doubl-f7gj | \n\n public Node flatten(Node head) {\n Node curr = head;\n while(curr!=null){\n if(curr.child!=null){\n Node next = | faanged | NORMAL | 2020-02-12T07:05:00.374122+00:00 | 2020-02-12T07:05:41.092809+00:00 | 1,805 | false | ```\n\n public Node flatten(Node head) {\n Node curr = head;\n while(curr!=null){\n if(curr.child!=null){\n Node next = flatten(curr.child);\n curr.child = null;\n Node dummyNext = next;\n next.prev = curr;\n while(ne... | 10 | 5 | [] | 7 |
flatten-a-multilevel-doubly-linked-list | Non recursive Simple C++ solution | non-recursive-simple-c-solution-by-aadit-u7d6 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | aadityajawanjal34 | NORMAL | 2023-02-24T07:18:49.218957+00:00 | 2023-02-24T07:18:49.218998+00:00 | 695 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 9 | 0 | ['C++'] | 1 |
flatten-a-multilevel-doubly-linked-list | Python Recursion: Easy-to-understand with Explanation | python-recursion-easy-to-understand-with-b31f | Intuition\n\nSince we are working with a doubly-linked list, for every node.child that we handle, we need to obtain the end node of the list at that particular | zayne-siew | NORMAL | 2021-10-31T06:47:50.253361+00:00 | 2021-10-31T06:47:50.253408+00:00 | 1,356 | false | ### Intuition\n\nSince we are working with a doubly-linked list, for every `node.child` that we handle, we need to obtain the end `node` of the list at that particular level, so that we can assign its `node.next` appropriately. We can thus write a recursive function that does the following:\n\n- Loop through each `node... | 9 | 0 | ['Recursion', 'Python', 'Python3'] | 1 |
flatten-a-multilevel-doubly-linked-list | [a,b,c]is not a valid doubly linked list. | abcis-not-a-valid-doubly-linked-list-by-fzoc6 | [a,b,c]is not a valid doubly linked list.\nIf you are getting the following error it is probably because you have not set the child of "flattened" node to NULL | vijayshank | NORMAL | 2021-04-10T19:45:39.279901+00:00 | 2021-04-10T19:45:39.279946+00:00 | 267 | false | `[a,b,c]is not a valid doubly linked list.`\nIf you are getting the following error it is probably because you have not set the child of "flattened" node to `NULL` | 9 | 0 | [] | 1 |
flatten-a-multilevel-doubly-linked-list | Javascript recursive and array | javascript-recursive-and-array-by-bloddy-2tpt | Walk recursively by the order of child and next\n2. Store path sequentially into an array\n3. Untangle the pointers (prev points to previous node next points to | bloddybear | NORMAL | 2019-10-22T17:44:26.163629+00:00 | 2019-10-22T17:44:26.163665+00:00 | 818 | false | 1. Walk recursively by the order of `child` and `next`\n2. Store path sequentially into an array\n3. Untangle the pointers (`prev` points to previous node `next` points to next node)\n```\n/**\n * @param {Node} head\n * @return {Node}\n */\nvar flatten = function(head) {\n const arr = [];\n const helper = (node) ... | 9 | 0 | ['Recursion', 'JavaScript'] | 1 |
flatten-a-multilevel-doubly-linked-list | Java Recursive Solution 6 lines 0 ms | java-recursive-solution-6-lines-0-ms-by-jc6wq | \nclass Solution {\n Node end = null;\n public Node flatten(Node head) {\n if(head == null) return end;\n\t\t// 0. head -> flatten(head.child) -> f | echo999 | NORMAL | 2019-08-20T04:43:36.528330+00:00 | 2019-08-20T04:45:47.202368+00:00 | 780 | false | ```\nclass Solution {\n Node end = null;\n public Node flatten(Node head) {\n if(head == null) return end;\n\t\t// 0. head -> flatten(head.child) -> flatten(head.next) -> end \n\t\t// 1. flatten(head.next) -> end \n end = flatten(head.next);\n\t\t// 2. head -> flatten(head.child) \n head.next... | 9 | 0 | ['Recursion', 'Java'] | 1 |
flatten-a-multilevel-doubly-linked-list | Different python solutions | different-python-solutions-by-otoc-tc2i | Solution 1: recursive solution\n\n def flatten(self, head: \'Node\') -> \'Node\':\n def recursive(h):\n p_prev, p = None, h\n wh | otoc | NORMAL | 2019-07-16T05:07:40.058607+00:00 | 2019-07-16T05:21:21.470490+00:00 | 710 | false | Solution 1: recursive solution\n```\n def flatten(self, head: \'Node\') -> \'Node\':\n def recursive(h):\n p_prev, p = None, h\n while p and not p.child:\n p_prev, p = p, p.next\n if not p:\n return h, p_prev\n else:\n p_... | 9 | 0 | [] | 3 |
flatten-a-multilevel-doubly-linked-list | [Python 3] Using recursion || beats 98% || 36ms 🥷🏼 | python-3-using-recursion-beats-98-36ms-b-clez | \npython3 []\nclass Solution:\n def flatten(self, head: \'Optional[Node]\') -> \'Optional[Node]\':\n def unpack(head):\n cur = tail = head\ | yourick | NORMAL | 2023-08-03T17:37:18.018783+00:00 | 2023-08-14T13:54:52.849544+00:00 | 1,010 | false | \n```python3 []\nclass Solution:\n def flatten(self, head: \'Optional[Node]\') -> \'Optional[Node]\':\n def unpack(head):\n cur = tail = head\n while cur:\n if cur.child:\n start, end = unpack(cur.child)\n if cur.next: cur.next.prev = ... | 8 | 0 | ['Linked List', 'Recursion', 'Python', 'Python3'] | 0 |
flatten-a-multilevel-doubly-linked-list | 430: Solution with step by step explanation | 430-solution-with-step-by-step-explanati-ojwj | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. Define the function flattenDFS which takes a Node object as input and | Marlen09 | NORMAL | 2023-03-07T18:23:18.024248+00:00 | 2023-03-07T18:23:18.024294+00:00 | 1,667 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define the function flattenDFS which takes a Node object as input and returns a Node object.\n2. Check if the node object is None. If it is, return None.\n3. Check if node object has no child and no next pointers. If it d... | 8 | 0 | ['Linked List', 'Depth-First Search', 'Doubly-Linked List', 'Python', 'Python3'] | 0 |
flatten-a-multilevel-doubly-linked-list | Easy Preorder Traversal of Binary Tree Approach (with diagram) | easy-preorder-traversal-of-binary-tree-a-ct3z | Consider the following Multilevel Linked List, with N denotes next pointer, P denotes previous pointer and C denotes Child Pointer!\n\n\n\nIn this example, the | kashish098 | NORMAL | 2022-09-01T19:10:06.276517+00:00 | 2022-09-01T19:12:03.976179+00:00 | 623 | false | Consider the following Multilevel Linked List, with N denotes next pointer, P denotes previous pointer and C denotes Child Pointer!\n\n\n\nIn this example, the answer will be [1,2,3,4,5,6,7]\n\nThis given multil... | 8 | 0 | ['Depth-First Search', 'Recursion', 'C', 'Binary Tree', 'Java'] | 0 |
flatten-a-multilevel-doubly-linked-list | JavaScript Iterative Solution| Easy- with example walkthrough | javascript-iterative-solution-easy-with-d4z48 | \n /*\n Time: O(n) || Space: O(n)\n Please upvote if you find implementation with example useful, \n i\'ll try to implement examples in my future posts as well | Gift369 | NORMAL | 2021-03-10T14:45:35.139773+00:00 | 2021-03-10T14:59:54.002356+00:00 | 517 | false | ```\n /*\n Time: O(n) || Space: O(n)\n Please upvote if you find implementation with example useful, \n i\'ll try to implement examples in my future posts as well :) \n*/\nvar flatten = function(head) {\n let temp = head;\n let stack = [];\n \n while (head) {\n if (head.child) {\n if (head... | 8 | 0 | ['Stack', 'Depth-First Search', 'Iterator', 'JavaScript'] | 0 |
flatten-a-multilevel-doubly-linked-list | C++ Easy Recursive Solution | c-easy-recursive-solution-by-nivedit-l4kz | ```\nclass Solution {\npublic:\n Node flatten(Node head) {\n if(!head) return head;\n Node curr=head;\n while(curr)\n {\n | nivedit | NORMAL | 2020-07-10T07:29:22.920864+00:00 | 2020-07-10T07:29:22.920914+00:00 | 795 | false | ```\nclass Solution {\npublic:\n Node* flatten(Node* head) {\n if(!head) return head;\n Node* curr=head;\n while(curr)\n {\n if(curr->child)\n {\n Node* temp = curr->next;\n Node* x = flatten(curr->child);\n x->prev= cur... | 8 | 0 | [] | 0 |
flatten-a-multilevel-doubly-linked-list | Python sol by recursion. 80%+ [w/ Visualization ] | python-sol-by-recursion-80-w-visualizati-upgr | Python sol by recursion\n\n---\n\nIllustration and Visualization:\n\n\n\n---\n\nImplemetation by recursion\n\n\nclass Solution:\n def flatten(self, head: \'N | brianchiang_tw | NORMAL | 2020-05-22T12:58:31.845088+00:00 | 2020-05-22T12:58:31.845138+00:00 | 1,081 | false | Python sol by recursion\n\n---\n\n**Illustration and Visualization**:\n\n\n\n---\n\n**Implemetation** by recursion\n\n```\nclass Solution:\n def flatten(self, head: \'Node\') -> \'Node\':\n \n def helper(head) -> \'None\':\n... | 8 | 1 | ['Recursion', 'Python', 'Python3'] | 0 |
flatten-a-multilevel-doubly-linked-list | Python Recursive Solution + Analysis | python-recursive-solution-analysis-by-ha-a2kq | The key idea is to recursively flatten sublists, given the head. Flattening a child sublist and flattening a tail (contents of head.next) sublist is the same pr | hai_dee | NORMAL | 2018-08-12T06:25:06.512060+00:00 | 2018-08-12T06:25:06.512130+00:00 | 523 | false | The key idea is to recursively flatten sublists, given the head. Flattening a child sublist and flattening a tail (contents of head.next) sublist is the same procedure. Once we\'ve made the recursive calls, we need to assemble the list so that it\'s of the form head->[result of child flattening]->[result of tail flatte... | 8 | 0 | [] | 2 |
flatten-a-multilevel-doubly-linked-list | simple java and python solution | simple-java-and-python-solution-by-keert-1hc6 | Java solution\n\nclass Solution {\n public Node flatten(Node head) \n {\n Node curr=head;\n while(curr!=null)\n {\n if(cur | keerthy0212 | NORMAL | 2021-10-31T14:30:00.744910+00:00 | 2021-11-01T03:43:02.539386+00:00 | 727 | false | **Java solution**\n```\nclass Solution {\n public Node flatten(Node head) \n {\n Node curr=head;\n while(curr!=null)\n {\n if(curr.child==null)//if the current node doesnt have a child then lets just move to the next node\n {\n curr=curr.next;\n ... | 7 | 0 | ['Python', 'Java'] | 1 |
flatten-a-multilevel-doubly-linked-list | C++ Super Simple, Short and Clean Iterative Solution, 4ms O(n) TC O(1) SC | c-super-simple-short-and-clean-iterative-djwh | \nclass Solution {\npublic:\n Node* flatten(Node* head) {\n Node *ptr = head, *tmp_next, *runner;\n \n while (ptr) {\n if (pt | yehudisk | NORMAL | 2021-07-31T21:34:11.561974+00:00 | 2021-07-31T21:34:11.562014+00:00 | 413 | false | ```\nclass Solution {\npublic:\n Node* flatten(Node* head) {\n Node *ptr = head, *tmp_next, *runner;\n \n while (ptr) {\n if (ptr->child) {\n \n // Save the current next and connect the child to next\n tmp_next = ptr->next;\n ... | 7 | 0 | ['C'] | 0 |
flatten-a-multilevel-doubly-linked-list | 430. Flatten a Multilevel Doubly Linked List most easiest sol c++ | 430-flatten-a-multilevel-doubly-linked-l-931c | \nNode* flatten(Node* head) {\n if(!head) return head;\n if(!head->next and !head->child)\n return head;\n Node *p=head;\n | LakshyaVijh | NORMAL | 2020-07-10T07:14:02.739806+00:00 | 2020-07-10T07:16:37.891869+00:00 | 305 | false | ```\nNode* flatten(Node* head) {\n if(!head) return head;\n if(!head->next and !head->child)\n return head;\n Node *p=head;\n while(p)\n {\n if(p->child)\n {\n Node *r=p->child,*q=p->next;\n p->next=r;\n r->... | 7 | 1 | [] | 0 |
flatten-a-multilevel-doubly-linked-list | 💯 Beats 100% || ✅ Easy to understand & Best solution ⬆️🆙 || O(n)💥👏🔥 | beats-100-easy-to-understand-best-soluti-z5o9 | Please upvote if my solution and efforts helped you.\n***\n\n\n\n# Code\n\nclass Solution {\n public Node flatten(Node head) {\n Node curr=head;\n | SumitMittal | NORMAL | 2024-06-17T17:14:35.909144+00:00 | 2024-06-17T17:14:35.909177+00:00 | 416 | false | # Please upvote if my solution and efforts helped you.\n***\n\n\n\n# Code\n```\nclass Solution {\n public Node flatten(Node head) {\n Node curr=head;\n while(curr!=null){\n if(cu... | 6 | 0 | ['Linked List', 'Doubly-Linked List', 'Java'] | 2 |
flatten-a-multilevel-doubly-linked-list | Java | 0ms | easy to understand | java-0ms-easy-to-understand-by-venkat089-b1kr | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Venkat089 | NORMAL | 2023-01-09T15:58:32.074604+00:00 | 2023-01-09T15:58:32.074638+00:00 | 1,004 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 6 | 0 | ['Java'] | 0 |
flatten-a-multilevel-doubly-linked-list | python || O(1) space || iterative || detailed explanation || very fast | python-o1-space-iterative-detailed-expla-k2cj | \n"""\n# Definition for a Node.\nclass Node:\n def __init__(self, val, prev, next, child):\n self.val = val\n self.prev = prev\n self.ne | Yared_betsega | NORMAL | 2022-07-27T11:51:24.773145+00:00 | 2022-07-27T11:51:24.773192+00:00 | 481 | false | ```\n"""\n# Definition for a Node.\nclass Node:\n def __init__(self, val, prev, next, child):\n self.val = val\n self.prev = prev\n self.next = next\n self.child = child\n"""\n\nclass Solution:\n def flatten(self, head: \'Optional[Node]\') -> \'Optional[Node]\': \n node = hea... | 6 | 0 | ['Iterator', 'Python'] | 0 |
flatten-a-multilevel-doubly-linked-list | 2 Different Ways, Iterative & Recursive, With Comments | 2-different-ways-iterative-recursive-wit-hjda | 1. Iterative using Stack\n\n\npublic class Solution { \n public Node Flatten(Node head) { \n var current = head;\n var stack = new Stack<Nod | keperkjr | NORMAL | 2021-10-31T04:36:12.477618+00:00 | 2021-10-31T05:40:25.152255+00:00 | 112 | false | **1. Iterative using Stack**\n\n```\npublic class Solution { \n public Node Flatten(Node head) { \n var current = head;\n var stack = new Stack<Node>();\n \n // Loop through nodes\n while (current != null) {\n \n // Check to see if node has child\n ... | 6 | 1 | [] | 1 |
flatten-a-multilevel-doubly-linked-list | C++ Simple list traversal without using stack/queue/DFS | c-simple-list-traversal-without-using-st-fxou | \nclass Solution {\npublic:\n Node* flatten(Node* head) {\n Node* lead=head;\n Node* ptr=NULL; \n Node* beta=NULL; // beta is chi | blue_jerry | NORMAL | 2020-09-14T18:24:00.863134+00:00 | 2020-09-14T18:24:00.863189+00:00 | 341 | false | ```\nclass Solution {\npublic:\n Node* flatten(Node* head) {\n Node* lead=head;\n Node* ptr=NULL; \n Node* beta=NULL; // beta is child node\n while(head){\n if(head->child){\n ptr=head->next;\n beta=head->child;\n head->child=... | 6 | 0 | ['C'] | 1 |
flatten-a-multilevel-doubly-linked-list | Java short and concise with stack | java-short-and-concise-with-stack-by-vij-puy2 | \npublic Node flatten(Node head) {\n if(head == null){\n return head;\n }\n \n Stack<Node> stack = new Stack<>();\n | vijai_a | NORMAL | 2020-07-10T14:31:33.480318+00:00 | 2020-07-10T14:33:26.647154+00:00 | 535 | false | ```\npublic Node flatten(Node head) {\n if(head == null){\n return head;\n }\n \n Stack<Node> stack = new Stack<>();\n stack.push(head);\n Node prev = null;\n \n while(!stack.empty()){\n Node node = stack.pop();\n if(node.next != n... | 6 | 0 | ['Java'] | 1 |
flatten-a-multilevel-doubly-linked-list | C# DFS | c-dfs-by-bacon-lj9i | \npublic class Solution {\n public Node Flatten(Node head) {\n if (head == null) return null;\n DFS(head);\n return head;\n }\n\n | bacon | NORMAL | 2019-07-14T18:11:00.615457+00:00 | 2019-07-14T18:11:00.615520+00:00 | 333 | false | ```\npublic class Solution {\n public Node Flatten(Node head) {\n if (head == null) return null;\n DFS(head);\n return head;\n }\n\n private Node DFS(Node head) {\n var cur = head;\n\n while (cur != null && cur.child == null) {\n cur = cur.next;\n }\n\n ... | 6 | 0 | [] | 1 |
flatten-a-multilevel-doubly-linked-list | 0ms solution || beats 100% || easy to understand || O(n) time complexity || O(1) space complexity | 0ms-solution-beats-100-easy-to-understan-5l0d | Intuition\n Describe your first thoughts on how to solve this problem. Intuition is to insert all child nodes between parent node and parent->next node and the | coder_adi_ | NORMAL | 2024-07-09T03:16:15.280987+00:00 | 2024-07-11T04:31:34.670733+00:00 | 386 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->Intuition is to insert all child nodes between parent node and parent->next node and the again check for child nodes from parent node\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Spac... | 5 | 0 | ['Linked List', 'C++'] | 1 |
flatten-a-multilevel-doubly-linked-list | 0ms simple 100% faster using recursion in java | 0ms-simple-100-faster-using-recursion-in-ry2l | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | bhargavsuhagiya99 | NORMAL | 2023-04-23T05:38:36.862530+00:00 | 2023-04-23T05:39:15.250146+00:00 | 758 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/*\n// Definition for a Node.\n... | 5 | 0 | ['Recursion', 'Java'] | 1 |
flatten-a-multilevel-doubly-linked-list | ✅ [C++] | Beginner Friendly | Two Solutions DFS & Iterative | O(1) | c-beginner-friendly-two-solutions-dfs-it-ab9c | Complexity\n- Time complexity: O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(n)\n Add your space complexity here, e.g. O(n) \n\n## D | psycho_programer | NORMAL | 2023-01-11T11:53:46.486585+00:00 | 2023-01-11T11:53:46.486635+00:00 | 703 | false | ## Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n## DFS\n```\nclass Solution {\npublic:\n vector<Node*> nodes;\n void dfs(Node* head) {\n if(!head) return;\n no... | 5 | 0 | ['C++'] | 0 |
flatten-a-multilevel-doubly-linked-list | very simple java beats 100% , beginner friendly | very-simple-java-beats-100-beginner-frie-00bz | \npublic Node flatten(Node head) {\n Node temp = head ;\n Node prev = null ;\n Node next = null ;\n Node child = null ;\n Sta | rinshu2510 | NORMAL | 2021-10-31T09:20:10.399288+00:00 | 2021-10-31T09:20:10.399335+00:00 | 290 | false | ```\npublic Node flatten(Node head) {\n Node temp = head ;\n Node prev = null ;\n Node next = null ;\n Node child = null ;\n Stack<Node> s = new Stack<Node>();\n while( temp != null || !s.isEmpty()){\n if(temp == null){\n next = s.pop();\n ... | 5 | 1 | [] | 2 |
flatten-a-multilevel-doubly-linked-list | Flatten a Multilevel Doubly Linked List || Java O(N) Solution using Recursion | flatten-a-multilevel-doubly-linked-list-htg43 | \npublic Node flatten(Node head) {\n helper(head);\n return head;\n }\n public Node helper(Node node){\n while(node != null){\n | ridhamsheel | NORMAL | 2021-09-01T08:24:50.220017+00:00 | 2021-09-01T08:24:50.220058+00:00 | 119 | false | ```\npublic Node flatten(Node head) {\n helper(head);\n return head;\n }\n public Node helper(Node node){\n while(node != null){\n if(node.child != null){ // If the current node has a child\n Node stored = node.next; // we store its old next node\n ... | 5 | 0 | [] | 1 |
flatten-a-multilevel-doubly-linked-list | C++ solution using stack (easy to understand) well commented + explanation | c-solution-using-stack-easy-to-understan-fj0t | main idea:: a stack is kept for the puspose of storing the the next value of a particular node which has a child node\n\n \n1---2---3---4---5---6--NULL\n | gunjan10 | NORMAL | 2020-07-10T11:21:17.777784+00:00 | 2020-07-10T11:21:17.777818+00:00 | 261 | false | main idea:: a stack is kept for the puspose of storing the the next value of a particular node which has a child node\n\n ```\n1---2---3---4---5---6--NULL\n |\n 7---8---9---10--NULL\n |\n 11--12--NULL\n```\n\nlet me explain with this example :: \nstep 1) when 1 is encountere... | 5 | 0 | ['Linked List', 'Doubly-Linked List'] | 1 |
flatten-a-multilevel-doubly-linked-list | Python DFS | python-dfs-by-liketheflower-6pba | DFS traverse the node and put everything into a list. The list will contain the correct order of the visited nodes.\nThen from the list rebuild a single level l | liketheflower | NORMAL | 2020-04-11T17:27:10.588748+00:00 | 2020-05-11T17:59:04.815406+00:00 | 329 | false | DFS traverse the node and put everything into a list. The list will contain the correct order of the visited nodes.\nThen from the list rebuild a single level linked list.\nThis algorithm has time complexity of O(n), however, the space complexity is also O(n). It is easy to understand.\n```python\nclass Solution:\n ... | 5 | 0 | [] | 1 |
flatten-a-multilevel-doubly-linked-list | Javascript 90% faster, 100% less memory. Recursive. | javascript-90-faster-100-less-memory-rec-oayr | The idea is the following: \n If a node has a child:\n1. Keep a reference of node.next as nextNode;\n1. Find the tail of node\'s child (implement a findTail fun | javgc | NORMAL | 2020-03-19T23:31:15.520587+00:00 | 2020-03-19T23:31:15.520639+00:00 | 518 | false | The idea is the following: \n* If a node has a child:\n1. Keep a reference of node.next as nextNode;\n1. Find the tail of node\'s child (implement a findTail function)\n1. The tail of node\'s child will have nextNode as its next.\n1. Change node.next to the child and set node.child = null;\n* Recursively do this over t... | 5 | 0 | ['Linked List', 'Recursion', 'JavaScript'] | 3 |
flatten-a-multilevel-doubly-linked-list | C++, Concise solution | Beats 98.6% | c-concise-solution-beats-986-by-naman_nm-kgwg | void func(Node head, Node q) {\n \n if(!head) return;\n \n func(head->next, q);\n if(q && !head->next)\n {\n | naman_nm | NORMAL | 2019-05-30T17:12:19.050736+00:00 | 2019-06-20T05:48:00.420319+00:00 | 1,158 | false | void func(Node* head, Node* q) {\n \n if(!head) return;\n \n func(head->next, q);\n if(q && !head->next)\n {\n head->next=q;\n q->prev = head;\n }\n \n \n if(head->child)\n {\n Node* temp = head->next;\n ... | 5 | 0 | ['C++'] | 1 |
flatten-a-multilevel-doubly-linked-list | C++ Recursive and Iterative | c-recursive-and-iterative-by-jianchao-li-j7t0 | The recursive idea is simple: each time when we reach a node with child, recursively flatten the child and insert it between the current node and its next node. | jianchao-li | NORMAL | 2019-02-03T13:46:24.528720+00:00 | 2019-02-03T13:46:24.528765+00:00 | 550 | false | The recursive idea is simple: each time when we reach a node with `child`, recursively flatten the child and insert it between the current node and its next node. Remember to take care of the `prev` pointers.\n\n```cpp\nclass Solution {\npublic:\n Node* flatten(Node* head) {\n Node* cur = head;\n while... | 5 | 0 | [] | 1 |
flatten-a-multilevel-doubly-linked-list | composite recursion method | composite-recursion-method-by-cissyyao-plp4 | Inspired by 114. Flatten Binary Tree to Linked List, using head and tail as parameters in recursive method. \n\nclass Solution {\n public Node flatten(Node h | cissyyao | NORMAL | 2018-07-23T19:47:31.331607+00:00 | 2018-08-21T20:52:47.297132+00:00 | 549 | false | Inspired by 114. Flatten Binary Tree to Linked List, using head and tail as parameters in recursive method. \n\nclass Solution {\n public Node flatten(Node head) { \n return flatten(head, null);\n }\n \n private Node flatten(Node head, Node tail){\n if (head == null) return tail;\n if (... | 5 | 0 | [] | 2 |
flatten-a-multilevel-doubly-linked-list | Java Easy to Understand Solution with Explanation || 100% beats | java-easy-to-understand-solution-with-ex-seua | Intuition\n> We can solve this easily using an iterative approach. When there is a child node, we need to point that child as the next node. For the end of that | leo_messi10 | NORMAL | 2024-05-30T08:16:32.250502+00:00 | 2024-05-30T08:16:32.250526+00:00 | 257 | false | # Intuition\n> We can solve this easily using an iterative approach. When there is a child node, we need to point that child as the next node. For the end of that child\'s sublist, we need to connect it to the next node of the main list.\n\n# Approach\n\n\n### Function Signature\n```java\npublic Node flatten(Node head)... | 4 | 0 | ['Linked List', 'Doubly-Linked List', 'Java'] | 3 |
flatten-a-multilevel-doubly-linked-list | 0 ms | 100% beat Solution | C++ | Very Easy Approach | 0-ms-100-beat-solution-c-very-easy-appro-z4w0 | Approach\n Describe your approach to solving the problem. \n1. Traverse the list while handling child pointers:\n - Iterate through the list nodes.\n - If | yshivhare163 | NORMAL | 2023-08-02T09:49:59.813283+00:00 | 2023-08-06T10:19:26.895441+00:00 | 397 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\n1. Traverse the list while handling child pointers:\n - Iterate through the list nodes.\n - If a node has a `child`, update the `next` and `child` pointers accordingly.\n - Collect the next node after the current level in a vector `nodes` (st... | 4 | 0 | ['Linked List', 'Stack', 'Depth-First Search', 'Doubly-Linked List', 'C++'] | 1 |
flatten-a-multilevel-doubly-linked-list | [Java] Easy 100% solution | java-easy-100-solution-by-ytchouar-nszi | java\n/*\n// Definition for a Node.\nclass Node {\n public int val;\n public Node prev;\n public Node next;\n public Node child;\n};\n*/\n\nclass So | YTchouar | NORMAL | 2023-07-10T04:20:51.125405+00:00 | 2023-07-10T04:20:51.125434+00:00 | 543 | false | ```java\n/*\n// Definition for a Node.\nclass Node {\n public int val;\n public Node prev;\n public Node next;\n public Node child;\n};\n*/\n\nclass Solution {\n public Node flatten(Node head) {\n Node curr = head;\n\n while(curr != null) {\n if(curr.child != null) {\n ... | 4 | 0 | ['Java'] | 0 |
flatten-a-multilevel-doubly-linked-list | Without recurssion c++ easy peasy solution | without-recurssion-c-easy-peasy-solution-ard5 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | underdogsourav | NORMAL | 2023-03-20T07:38:52.278646+00:00 | 2023-03-20T07:38:52.278700+00:00 | 633 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 4 | 0 | ['C++'] | 0 |
flatten-a-multilevel-doubly-linked-list | Simple solution using stack || C++ | simple-solution-using-stack-c-by-rkkumar-db0i | Please upvote if you like my solution .\n\nclass Solution {\npublic:\n Node* flatten(Node* head) {\n stack<Node*> st;\n if(head == NULL) retur | rkkumar421 | NORMAL | 2022-07-17T05:39:12.222578+00:00 | 2022-07-17T05:39:12.222625+00:00 | 211 | false | Please upvote if you like my solution .\n```\nclass Solution {\npublic:\n Node* flatten(Node* head) {\n stack<Node*> st;\n if(head == NULL) return head;\n Node* ptr = head;\n \n while(ptr->next != NULL || ptr->child != NULL){\n if(ptr->child != NULL){\n s... | 4 | 0 | ['Stack', 'C', 'Iterator'] | 1 |
flatten-a-multilevel-doubly-linked-list | Flatten a Multilevel Doubly Linked List | c++ recursive/recursion SOLUTION | 8ms | flatten-a-multilevel-doubly-linked-list-jieos | \nclass Solution {\npublic:\n // THINK OF THE PROBLEM AS NODES AS ROOMS PRESENT IN DIFFERENT DEPTHS/LEVELS OF AN\n // HIGH RISE BUILDING\n // Naturally | looneyd_noob | NORMAL | 2022-07-12T12:39:25.155039+00:00 | 2022-07-12T12:39:25.155094+00:00 | 204 | false | ```\nclass Solution {\npublic:\n // THINK OF THE PROBLEM AS NODES AS ROOMS PRESENT IN DIFFERENT DEPTHS/LEVELS OF AN\n // HIGH RISE BUILDING\n // Naturally we need to traverse the list, and hence we do that\n // Whenever we encounter any child node, we recursively go to the depth and make\n // changes to ... | 4 | 0 | ['Linked List', 'Recursion'] | 2 |
flatten-a-multilevel-doubly-linked-list | Easy Recursive and Iterative solutions | easy-recursive-and-iterative-solutions-b-krwc | Iterative:\n\n\nvar flatten = function(head) {\n const stack = [];\n let ptr = head;\n \n while(ptr || stack.length) {\n if(ptr.child) {\n | dollysingh | NORMAL | 2022-05-14T10:35:18.771316+00:00 | 2022-05-14T10:35:18.771356+00:00 | 447 | false | **Iterative**:\n\n```\nvar flatten = function(head) {\n const stack = [];\n let ptr = head;\n \n while(ptr || stack.length) {\n if(ptr.child) {\n if(ptr.next)\n stack.push(ptr.next);\n let child = ptr.child;\n ptr.child = null;\n ptr.next = c... | 4 | 0 | ['Recursion', 'Iterator', 'JavaScript'] | 1 |
flatten-a-multilevel-doubly-linked-list | Java recursive, beat 100%, similar to flatten tree | java-recursive-beat-100-similar-to-flatt-3ajk | Think of structure as being a binary tree structure. Then you can solve it similarly to the flatten tree problem: https://leetcode.com/problems/flatten-binary-t | jbuns | NORMAL | 2022-05-09T21:56:33.078718+00:00 | 2022-05-09T21:56:33.078747+00:00 | 394 | false | Think of structure as being a binary tree structure. Then you can solve it similarly to the flatten tree problem: https://leetcode.com/problems/flatten-binary-tree-to-linked-list/\n\n\n```\nclass Solution {\n \n Node prev = null;\n \n public Node flatten(Node head) {\n dfsHelper(head);\n retur... | 4 | 0 | ['Depth-First Search', 'Recursion', 'Java'] | 0 |
flatten-a-multilevel-doubly-linked-list | JAVA Recursion Code or DFS 100% faster with Explaination | java-recursion-code-or-dfs-100-faster-wi-8yjo | use of simple dfs firstOrder\n\n\n/*\n// Definition for a Node.\nclass Node {\n public int val;\n public Node prev;\n public Node next;\n public Nod | VIVEK_8877 | NORMAL | 2021-10-31T02:13:03.968484+00:00 | 2022-03-17T04:12:43.293929+00:00 | 333 | false | use of simple dfs firstOrder\n\n```\n/*\n// Definition for a Node.\nclass Node {\n public int val;\n public Node prev;\n public Node next;\n public Node child;\n};\n*/\n\nclass Solution {\n public static Node ans;\n public static Node help; // used to keep at top of node\n public static void DFS(No... | 4 | 1 | [] | 0 |
flatten-a-multilevel-doubly-linked-list | Easy to understand 4ms code | easy-to-understand-4ms-code-by-ladyengg-qliy | \nNode *flatten(Node *head)\n {\n\n Node *temp = head;\n\n while (temp != NULL)\n {\n if (temp->child)\n {\n | ladyengg | NORMAL | 2021-06-08T18:59:56.475300+00:00 | 2021-06-08T18:59:56.475351+00:00 | 273 | false | ```\nNode *flatten(Node *head)\n {\n\n Node *temp = head;\n\n while (temp != NULL)\n {\n if (temp->child)\n {\n Node *node_child = temp->child;\n if (temp->next == NULL)\n {\n temp->next = node_child;\n ... | 4 | 0 | ['C'] | 0 |
flatten-a-multilevel-doubly-linked-list | Elegant Iterative Solution | elegant-iterative-solution-by-ysingla-fsbq | \nclass Solution:\n def traverseChild(self, head):\n tail = head\n while tail and tail.next:\n tail = tail.next\n return tail | ysingla | NORMAL | 2020-11-25T04:35:28.692203+00:00 | 2020-11-25T04:36:58.614717+00:00 | 293 | false | ```\nclass Solution:\n def traverseChild(self, head):\n tail = head\n while tail and tail.next:\n tail = tail.next\n return tail\n \n def flatten(self, head: \'Node\') -> \'Node\':\n curr = head\n while curr:\n if curr.child:\n tail = self... | 4 | 0 | ['Iterator', 'Python', 'Python3'] | 0 |
flatten-a-multilevel-doubly-linked-list | Easy solution 99% fast [C++] | easy-solution-99-fast-c-by-meyrlan-7g73 | \nclass Solution {\npublic:\n \n vector < Node > v;\n \n void dfs(Node head){\n v.push_back(head);\n if(head -> child != nullptr){\n df | meyrlan | NORMAL | 2020-08-14T08:52:31.022014+00:00 | 2020-08-14T08:52:31.022046+00:00 | 117 | false | \nclass Solution {\npublic:\n \n vector < Node* > v;\n \n void dfs(Node* head){\n v.push_back(head);\n if(head -> child != nullptr){\n dfs(head -> child);\n }\n if(head -> next != nullptr){\n dfs(head -> next);\n }\n }\n \n Node* flatten(Node* head) {\n \n ... | 4 | 0 | [] | 1 |
flatten-a-multilevel-doubly-linked-list | Java Solution using a Stack | java-solution-using-a-stack-by-pirty6-z7aq | We can just use a stack to pile the next nodes when there is a child node. For it to be valid remember to set the child node to null and change the prev pointer | pirty6 | NORMAL | 2020-07-11T01:23:05.488613+00:00 | 2020-07-11T01:23:05.488654+00:00 | 256 | false | We can just use a stack to pile the next nodes when there is a child node. For it to be valid remember to set the child node to null and change the prev pointers (while validating that they are not null)\n\n```\n public Node flatten(Node head) {\n Stack<Node> prevs = new Stack();\n Node cur = head;\n ... | 4 | 1 | [] | 0 |
flatten-a-multilevel-doubly-linked-list | Java - Two Pointers - 0 ms | java-two-pointers-0-ms-by-mt-xtina-mwjw | O(n) time, O(1) space (where n is the total number of nodes in the linked list).\n\nWe traverse until we find a node with a child - let\'s call it parent. From | mt-xtina | NORMAL | 2020-07-10T15:41:19.161166+00:00 | 2020-07-10T15:43:47.529854+00:00 | 260 | false | O(n) time, O(1) space (where n is the total number of nodes in the linked list).\n\nWe traverse until we find a node with a child - let\'s call it parent. From here, we set up a few variables: the parent\'s original next node, and the parent\'s child. Additionally, we traverse the child list to find its last node. Firs... | 4 | 0 | ['Two Pointers', 'Java'] | 1 |
flatten-a-multilevel-doubly-linked-list | recursive java solution, beats 100% | recursive-java-solution-beats-100-by-vsa-yn0u | \nclass Solution {\n public Node flatten(Node head) {\n if(head == null) return null;\n recursiveHelper(head);\n return head;\n }\n | vsavko | NORMAL | 2020-07-10T08:22:58.726616+00:00 | 2020-07-10T08:22:58.726663+00:00 | 213 | false | ```\nclass Solution {\n public Node flatten(Node head) {\n if(head == null) return null;\n recursiveHelper(head);\n return head;\n }\n \n private Node recursiveHelper(Node head){\n Node tmp = head.next;\n if(head.child != null){\n head.next = head.child;\n ... | 4 | 2 | [] | 0 |
flatten-a-multilevel-doubly-linked-list | [c++] Stack solution with explanation | c-stack-solution-with-explanation-by-sli-um9s | simple explanation: as we go along our list, if we reach a node where its child is not a null pointer then we push the next node on to our stack and set the nex | slicedkiwis | NORMAL | 2020-07-10T07:22:43.456419+00:00 | 2020-07-11T05:54:28.527649+00:00 | 288 | false | simple explanation: as we go along our list, if we reach a node where its child is not a null pointer then we push the next node on to our stack and set the next node to the child node. Of course we also have to set the child to a null pointer, and set our next nodes previous node to the current node.\n\nWe repeat this... | 4 | 2 | ['Stack', 'C', 'C++'] | 1 |
flatten-a-multilevel-doubly-linked-list | Easy to understand C++ Solution, 88ms, beats 95% | easy-to-understand-c-solution-88ms-beats-lc2c | Runtime: 88 ms, faster than 95.78% of C++ online submissions for Flatten a Multilevel Doubly Linked List.\nMemory Usage: 31.1 MB, less than 62.50% of C++ online | pooja0406 | NORMAL | 2019-10-22T20:58:38.690818+00:00 | 2019-10-22T20:58:38.690869+00:00 | 426 | false | Runtime: 88 ms, faster than 95.78% of C++ online submissions for Flatten a Multilevel Doubly Linked List.\nMemory Usage: 31.1 MB, less than 62.50% of C++ online submissions for Flatten a Multilevel Doubly Linked List.\n\n```\n/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* prev;\n Node*... | 4 | 0 | ['Recursion'] | 0 |
flatten-a-multilevel-doubly-linked-list | Java solution | java-solution-by-lliu97d-j7ys | \nclass Solution {\n public Node flatten(Node head) {\n Node p = head;\n while(p != null){\n if(p.child != null){\n N | lliu97d | NORMAL | 2019-07-09T02:36:46.315749+00:00 | 2019-07-09T02:36:46.315778+00:00 | 333 | false | ```\nclass Solution {\n public Node flatten(Node head) {\n Node p = head;\n while(p != null){\n if(p.child != null){\n Node tmp = p.child;\n while(tmp.next != null)\n tmp = tmp.next;\n tmp.next = p.next;\n if(p.ne... | 4 | 0 | ['Java'] | 0 |
flatten-a-multilevel-doubly-linked-list | Simple Java Solution | simple-java-solution-by-waiyip-16d9 | with global variable\n\nclass Solution {\n Node prev;\n public Node flatten(Node head) {\n Node cur = head;\n while (cur != null) {\n | waiyip | NORMAL | 2018-06-09T02:31:43.554688+00:00 | 2018-06-09T02:31:43.554688+00:00 | 1,935 | false | with global variable\n```\nclass Solution {\n Node prev;\n public Node flatten(Node head) {\n Node cur = head;\n while (cur != null) {\n if (cur.child != null) {\n Node next = cur.next;\n cur.child.prev = cur;\n cur.next = flatten(cur.child);\n cu... | 4 | 1 | [] | 2 |
flatten-a-multilevel-doubly-linked-list | ✅EASY JAVA SOLUTION | easy-java-solution-by-swayam28-dohs | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | swayam28 | NORMAL | 2024-08-21T03:21:35.506676+00:00 | 2024-08-21T03:21:35.506713+00:00 | 293 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | 0 | ['Queue', 'Java'] | 0 |
flatten-a-multilevel-doubly-linked-list | ⬆️🆙✅ Easy to understand & Best solution 💥👏🔥 | up-easy-to-understand-best-solution-by-s-us3d | Please upvote if my solution and efforts helped you.\n***\n\n# Code\n\nclass Solution {\n public Node flatten(Node head) {\n Node temp = head;\n | SumitMittal | NORMAL | 2024-06-17T17:03:48.876414+00:00 | 2024-06-17T17:03:48.876442+00:00 | 366 | false | # Please upvote if my solution and efforts helped you.\n***\n\n# Code\n```\nclass Solution {\n public Node flatten(Node head) {\n Node temp = head;\n while(temp != null){\n Node list1Tail = temp;\n Node list3Head = temp.next;\n // if the node has child node then append ... | 3 | 0 | ['Linked List', 'Doubly-Linked List', 'Java'] | 0 |
flatten-a-multilevel-doubly-linked-list | Best code || easy to understand || must see | best-code-easy-to-understand-must-see-by-egwh | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Shubhadip_009 | NORMAL | 2024-06-10T10:57:06.239781+00:00 | 2024-06-10T10:57:06.239832+00:00 | 337 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | 0 | ['C++'] | 0 |
flatten-a-multilevel-doubly-linked-list | Java | Recursion | 0 ms | Time: O(n) | Space: O(1) | java-recursion-0-ms-time-on-space-o1-by-dzx89 | Intuition\nWe iterate the list untill we reach a node which has child node, when we reach it, we will call the function again to flatten the child too.\nAnd the | leetcodeally | NORMAL | 2024-01-03T17:11:42.342796+00:00 | 2024-01-03T17:11:42.342822+00:00 | 558 | false | # Intuition\nWe iterate the list untill we reach a node which has child node, when we reach it, we will call the function again to flatten the child too.\nAnd the function will return the last node of the child as we need to connnect it with next node of curent node. The process continues untill we reach end of the lis... | 3 | 0 | ['Java'] | 2 |
flatten-a-multilevel-doubly-linked-list | Implementation using stack and dummy pointer [99% Space Complexity] | implementation-using-stack-and-dummy-poi-tt3d | Intuition\n Describe your first thoughts on how to solve this problem. \nMy First thoughts upon viewing the questions is to some how save the head.next nodes wh | bhanuprabhath123 | NORMAL | 2023-11-06T09:34:23.026597+00:00 | 2023-11-06T09:34:23.026625+00:00 | 121 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nMy First thoughts upon viewing the questions is to some how save the `head.next` nodes when encountering the nodes with the `head.child` nodes. As saving the `head.next` once we continue moving forward in the child node and reach the end ... | 3 | 0 | ['Two Pointers', 'Stack', 'Python3'] | 0 |
flatten-a-multilevel-doubly-linked-list | O(n) Solution in one function. TypeScript/JavaScript | on-solution-in-one-function-typescriptja-csvl | Intuition\nSolution in one function\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nflatten function that recursively flattens a li | evlbon | NORMAL | 2023-10-29T11:44:41.439847+00:00 | 2023-10-29T11:44:41.439871+00:00 | 33 | false | # Intuition\nSolution in one function\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n`flatten` function that recursively flattens a linked list. Has a `next` parameter to link the flattened child list to the next node of the parent list, null if the parent list is missing.\n\n<!-- D... | 3 | 0 | ['TypeScript'] | 1 |
flatten-a-multilevel-doubly-linked-list | Unlocking the Mysteries of the Squiggly List: A Tale of Flattening Multilevel Doubly Linked Lists | unlocking-the-mysteries-of-the-squiggly-tbecz | Intuition\nWhen reading the question to flatten a multilevel doubly linked list, a first intuition would be to use recursion or iteration to traverse the list a | ajayramani | NORMAL | 2023-06-24T17:54:17.198764+00:00 | 2023-06-24T17:57:47.673089+00:00 | 872 | false | # Intuition\nWhen reading the question to flatten a multilevel doubly linked list, a first intuition would be to use recursion or iteration to traverse the list and handle the child nodes.\n\n# Approach\nThe approach used in this code is a recursive traversal of the multilevel doubly linked list to flatten it.\nThe fla... | 3 | 0 | ['Linked List', 'Python', 'Python3'] | 0 |
flatten-a-multilevel-doubly-linked-list | using stack simple approch | using-stack-simple-approch-by-deviltrek-mn1t | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | deviltrek | NORMAL | 2023-05-08T14:29:31.882420+00:00 | 2023-05-08T14:29:31.882462+00:00 | 257 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | 0 | ['C++'] | 0 |
flatten-a-multilevel-doubly-linked-list | Simple Clean & Clear | simple-clean-clear-by-abhishek1210aka-rtah | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Abhishek1210AkA | NORMAL | 2023-04-13T14:12:29.547171+00:00 | 2023-04-13T14:12:29.547215+00:00 | 375 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | 0 | ['C++'] | 0 |
flatten-a-multilevel-doubly-linked-list | Python simple solution beats 100% || Explained | python-simple-solution-beats-100-explain-qjkq | \n# Code\n\n\nclass Solution(object):\n def flatten(self, head):\n if not head:\n return head\n current_node = head\n while c | TomSavage | NORMAL | 2023-01-22T08:06:29.288704+00:00 | 2023-01-22T08:06:29.288736+00:00 | 1,099 | false | \n# Code\n```\n\nclass Solution(object):\n def flatten(self, head):\n if not head:\n return head\n current_node = head\n while current_node != None:\n if current_node.child == None:\n current_node = current_node.next\n else:\n tail_c... | 3 | 0 | ['Linked List', 'Doubly-Linked List', 'Python'] | 0 |
flatten-a-multilevel-doubly-linked-list | C++ | Simple Recursion | c-simple-recursion-by-ravi_raj_k-b4hp | Intuition\n Describe your first thoughts on how to solve this problem. \nWe can iterate through our linked list while adding Child Lists in between after flatte | Ravi_Raj_K | NORMAL | 2022-12-08T17:58:46.955424+00:00 | 2022-12-08T18:01:06.835223+00:00 | 937 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe can iterate through our linked list while adding **Child Lists** in between after flattening them using simple Recursion.\n\n# Approach\nWhenever in our list, if we found a node with child list:\n1) Save the **next** node\n2) Flatten t... | 3 | 0 | ['Recursion', 'C++'] | 0 |
flatten-a-multilevel-doubly-linked-list | ✅ Java | 2 Solutions - Iterative, Recursive | java-2-solutions-iterative-recursive-by-x2bwy | Approach 1: Recursive\n\n\n// Time complexity: O(n)\n// Space complexity: O(n)\n\nclass Solution {\n public Node flatten(Node head) {\n\t\thelper(head, null) | prashantkachare | NORMAL | 2022-09-30T16:03:10.237937+00:00 | 2022-09-30T16:03:10.237976+00:00 | 414 | false | **Approach 1: Recursive**\n\n```\n// Time complexity: O(n)\n// Space complexity: O(n)\n\nclass Solution {\n public Node flatten(Node head) {\n\t\thelper(head, null);\n return head;\n }\n\n public Node helper(Node curr, Node prev) {\n if (curr == null)\n return prev;\n \n ... | 3 | 0 | [] | 0 |
flatten-a-multilevel-doubly-linked-list | C++ solution with explanation | c-solution-with-explanation-by-maitreya4-562t | Original solution: https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/152066/c%2B%2B-about-10-lines-solution\nI will try to explain t | maitreya47 | NORMAL | 2022-09-27T22:50:50.395290+00:00 | 2022-09-27T22:50:50.395328+00:00 | 565 | false | Original solution: https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/152066/c%2B%2B-about-10-lines-solution\nI will try to explain the logic behind the code. Here we are iterating though our doubly linked list, if the current node does not have a child, go on. If it does, we first store the ... | 3 | 0 | ['C', 'Iterator', 'C++'] | 0 |
flatten-a-multilevel-doubly-linked-list | Java recursive solution : Similar to flatten a binary tree | java-recursive-solution-similar-to-flatt-4r64 | Notice that if we perform a preorder traversal of the linked list, we would have the nodes in the desired order. In other words, if we perform a \'reverse posto | akil003 | NORMAL | 2022-08-31T15:10:11.310881+00:00 | 2022-08-31T15:10:11.310922+00:00 | 160 | false | Notice that if we perform a preorder traversal of the linked list, we would have the nodes in the desired order. In other words, if we perform a \'reverse postorder\', we would get the nodes in reverse order. We would keep track of those nodes using \'prev\' and append them to the next node we get from the call stack. ... | 3 | 0 | ['Recursion'] | 0 |
flatten-a-multilevel-doubly-linked-list | Where is C? | where-is-c-by-bezlant-81j5 | Could we add C to the list of languages we can submit this problem, please? | bezlant | NORMAL | 2022-07-27T04:21:39.592631+00:00 | 2022-07-27T04:21:39.592669+00:00 | 102 | false | Could we add C to the list of languages we can submit this problem, please? | 3 | 0 | ['C'] | 0 |
flatten-a-multilevel-doubly-linked-list | JAVA Solution || 100% faster | java-solution-100-faster-by-gavadiya-i8b3 | \nclass Solution {\n public Node flatten(Node head) {\n \n if(head == null ){\n return head;\n }\n Stack<Node> stk = n | Gavadiya | NORMAL | 2022-07-01T17:02:11.121539+00:00 | 2022-07-01T17:02:11.121590+00:00 | 213 | false | ```\nclass Solution {\n public Node flatten(Node head) {\n \n if(head == null ){\n return head;\n }\n Stack<Node> stk = new Stack<>();\n Node temp = head;\n \n while(temp.next != null || temp.child != null ){\n \n if(temp.child != null... | 3 | 0 | ['Stack', 'Java'] | 1 |
flatten-a-multilevel-doubly-linked-list | EASY CPP CODE || 0 ms, faster than 100.00% | easy-cpp-code-0-ms-faster-than-10000-by-7xh3v | \n/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* prev;\n Node* next;\n Node* child;\n};\n*/\n\nclass Solution {\npublic:\n | user8782zV | NORMAL | 2022-06-27T18:28:30.628521+00:00 | 2022-06-27T18:28:30.628568+00:00 | 71 | false | ```\n/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* prev;\n Node* next;\n Node* child;\n};\n*/\n\nclass Solution {\npublic:\n \n Node* flatten(Node* head , Node* tail = NULL) {\n // to solve this question we will introduce tail to the list \n if ( !head ) retur... | 3 | 0 | [] | 0 |
flatten-a-multilevel-doubly-linked-list | ✅ Python - Simple - Recursion | python-simple-recursion-by-fractal393-b3mx | Concept: Recursion - insert the node in the list not the val, then create a new linked list and return the head of the linked list.\n\n def flatten(self, hea | Fractal393 | NORMAL | 2022-04-10T19:34:08.088910+00:00 | 2022-04-10T19:35:45.513105+00:00 | 165 | false | Concept: Recursion - insert the node in the list not the val, then create a new linked list and return the head of the linked list.\n\n def flatten(self, head: \'Optional[Node]\') -> \'Optional[Node]\':\n \n ans = []\n if not head:\n return head\n \n def traverse(node):\... | 3 | 0 | ['Linked List', 'Recursion', 'Python'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.