post_href stringlengths 57 213 | python_solutions stringlengths 71 22.3k | slug stringlengths 3 77 | post_title stringlengths 1 100 | user stringlengths 3 29 | upvotes int64 -20 1.2k | views int64 0 60.9k | problem_title stringlengths 3 77 | number int64 1 2.48k | acceptance float64 0.14 0.91 | difficulty stringclasses 3
values | __index_level_0__ int64 0 34k |
|---|---|---|---|---|---|---|---|---|---|---|---|
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/1979011/Python-solution-40ms | class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if(root==None):
return []
stack=[]
stack.append(root)
ans=[]
while stack:
curr=stack.pop()
ans.append(curr.val)
if(curr.left):
... | binary-tree-postorder-traversal | Python solution 40ms | bad_karma25 | 2 | 130 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,100 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/867376/Iterative-solution-using-1-stack-with-explanation | class Solution:
def postorderTraversal(self, root: TreeNode) -> List[int]:
ans, s = [], []
curr = root
while curr or s:
while curr:
s.append((curr, curr.right))
curr = curr.left
p = s.pop()
... | binary-tree-postorder-traversal | Iterative solution using 1 stack with explanation | ivmarkp | 2 | 398 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,101 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/2084279/Python3-DFS-Recursive-Solution | class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
res=[]
def dfs(root):
if not root:
return
dfs(root.left)
dfs(root.right)
res.append(root.val)
dfs(ro... | binary-tree-postorder-traversal | Python3 DFS Recursive Solution | rohith4pr | 1 | 71 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,102 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/1994763/Python-solution | class Solution:
def postorderTraversal(self, root):
traversal, stack = [], [root]
while stack:
node = stack.pop()
if node:
traversal.append(node.val)
stack.append(node.left)
stack.append(node.right)
return traversal[::-1... | binary-tree-postorder-traversal | Python solution | nomanaasif9 | 1 | 87 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,103 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/1430634/Python3-Pre-Order-Post-Order-Inorder-Solution-through-recursion | class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
res = []
self.dfs(root, res)
return res
def dfs(self, root, res):
if root:
self.dfs(root.left, res)
self.dfs(root.right, res)
res.append(root.val) | binary-tree-postorder-traversal | Python3 Pre Order, Post Order, Inorder Solution through recursion | Sanyamx1x | 1 | 287 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,104 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/1430634/Python3-Pre-Order-Post-Order-Inorder-Solution-through-recursion | class Solution:
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
res = []
self.dfs(root, res)
return res
def dfs(self, root, res):
if root:
self.dfs(root.left, res)
res.append(root.val)
self.dfs(root.right, res) | binary-tree-postorder-traversal | Python3 Pre Order, Post Order, Inorder Solution through recursion | Sanyamx1x | 1 | 287 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,105 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/251415/clean-python-both-recursive-and-iterative-solutions | class Solution(object):
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
rst = []
self.helper(root, rst)
return rst
def helper(self, node, rst):
if node:
self.helper(node.left, rst)
self.he... | binary-tree-postorder-traversal | clean python both recursive and iterative solutions | mazheng | 1 | 138 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,106 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/251415/clean-python-both-recursive-and-iterative-solutions | class Solution(object):
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
rst = []
stack = []
while root or stack:
if root:
rst.insert(0, root.val)
stack.append(root)
... | binary-tree-postorder-traversal | clean python both recursive and iterative solutions | mazheng | 1 | 138 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,107 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/2837835/python-beats-97.37 | class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
left = right = []
if root.left:
left = self.postorderTraversal(root.left)
if root.right:
right = self.postorderTraversal(root.right)
... | binary-tree-postorder-traversal | [python] - beats 97.37% | ceolantir | 0 | 4 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,108 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/2828864/python-oror-simple-solution-oror-recursion | class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
# if tree empty
if not root:
return []
# answer list
ans = []
def traverse(node: Optional[TreeNode]) -> None:
# if empty node
if not node... | binary-tree-postorder-traversal | python || simple solution || recursion | wduf | 0 | 2 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,109 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/2789117/Python-one-liner | class Solution:
def postorderTraversal(self, root):
return self.postorderTraversal(root.left) + self.postorderTraversal(root.right) + [root.val] if root else [] | binary-tree-postorder-traversal | Python one-liner | Pirmil | 0 | 3 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,110 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/2647141/Python-PostOrder-Recursion | class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
lst = []
return self.helper(root, lst)
def helper(self, root, lst):
if root:
self.helper(root.left, lst)
self.helper(root.right, lst)
lst.ap... | binary-tree-postorder-traversal | Python PostOrder Recursion | axce1 | 0 | 31 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,111 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/2646457/Ans-to-follow-up%3A-Iterative-straight-forward-Python-solution-same-logic-as-recursion | class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
stack = [root]
result = []
while stack:
node = stack.pop()
if node.left:
stack.append(node.left)
if node.right:... | binary-tree-postorder-traversal | Ans to follow-up: Iterative straight-forward Python solution, same logic as recursion 🫣 | saadbash | 0 | 3 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,112 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/2502131/python3-recursive-with-comments | class Solution:
'''
Postorder traversal is left to right, bottom to top, visiting root last
1. recursively traverse left subtree
2. recursively traverse right subtree
3. visit root node
'''
def recursivetraverse(self, root, output):
if root:
self.recursivetraverse(root.le... | binary-tree-postorder-traversal | python3 recursive with comments | shwetachandole | 0 | 14 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,113 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/2502128/Python3-iterativerecursive-with-comment | class Solution:
# Postorder traversal is left to right, bottom to top, visiting root last
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
self.output = []
def check_node(node):
if node.left:
check_node(node.left)
if node.right:
... | binary-tree-postorder-traversal | Python3 iterative/recursive with comment | shwetachandole | 0 | 34 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,114 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/2352724/Python-simple-iterative-with-explanation | class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
# Init store
ans = deque()
# Init stack
stack = [root]
# Iterate through stack
while stack:
node = stack.pop()
# Base case
if not node:
... | binary-tree-postorder-traversal | Python simple iterative with explanation | drblessing | 0 | 66 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,115 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/2314230/Python-Time%3A-69-ms-oror-Mem%3A-13.8MB-oror-Recursive-Easy-Understand | class Solution:
def __init__(self):
self.mylist: List[int] = []
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if root:
self.postorderTraversal(root.left)
self.postorderTraversal(root.right)
self.mylist.append(root.val)
return sel... | binary-tree-postorder-traversal | [Python] Time: 69 ms || Mem: 13.8MB || Recursive Easy Understand | Buntynara | 0 | 37 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,116 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/2294305/python3-oror-easy-oror-2-stack-iterative-approach | class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if root is None:
return root
stack=[root]
stack2=[]
ans=[]
while stack!=[]:
node=stack.pop()
stack2.append(node.val)
if node.left is n... | binary-tree-postorder-traversal | python3 || easy || 2 stack iterative approach | _soninirav | 0 | 85 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,117 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/1930210/Python-Functional-or-Stack-or-Morris-Traversal-Multiple-Solutions! | class Solution:
def postorderTraversal(self, root):
self.res = []
self.helper(root)
return self.res
def helper(self, root):
if root:
self.helper(root.left)
self.helper(root.right)
self.res.append(root.val) | binary-tree-postorder-traversal | Python - Functional | Stack | Morris Traversal - Multiple Solutions! | domthedeveloper | 0 | 96 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,118 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/1930210/Python-Functional-or-Stack-or-Morris-Traversal-Multiple-Solutions! | class Solution:
def postorderTraversal(self, root):
return self.postorderTraversal(root.left) + self.postorderTraversal(root.right) + [root.val] if root else [] | binary-tree-postorder-traversal | Python - Functional | Stack | Morris Traversal - Multiple Solutions! | domthedeveloper | 0 | 96 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,119 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/1930210/Python-Functional-or-Stack-or-Morris-Traversal-Multiple-Solutions! | class Solution:
def postorderTraversal(self, root):
return [*self.postorderTraversal(root.left), *self.postorderTraversal(root.right), root.val] if root else [] | binary-tree-postorder-traversal | Python - Functional | Stack | Morris Traversal - Multiple Solutions! | domthedeveloper | 0 | 96 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,120 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/1930210/Python-Functional-or-Stack-or-Morris-Traversal-Multiple-Solutions! | class Solution:
def postorderTraversal(self, root):
res, stack = deque(), [root]
while stack:
node = stack.pop()
if node:
res.appendleft(node.val)
stack.append(node.left)
stack.append(node.right)
return res | binary-tree-postorder-traversal | Python - Functional | Stack | Morris Traversal - Multiple Solutions! | domthedeveloper | 0 | 96 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,121 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/1930210/Python-Functional-or-Stack-or-Morris-Traversal-Multiple-Solutions! | class Solution:
def postorderTraversal(self, root):
res = deque()
while root:
if root.right:
temp = root.right
while temp.left and temp.left != root:
temp = temp.left
if not temp.left:
res.appendleft... | binary-tree-postorder-traversal | Python - Functional | Stack | Morris Traversal - Multiple Solutions! | domthedeveloper | 0 | 96 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,122 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/1856712/Iterative-Simple-Solution-using-stack-and-deque-in-Python | class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
stack = []
result = deque()
if root:
stack.append(root)
while len(stack):
node = stack.pop()
result.appendleft(node.val)
if node.left:
... | binary-tree-postorder-traversal | Iterative Simple Solution using stack and deque in Python | anmolg | 0 | 49 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,123 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/1803481/Python-Simple-Python-Solution-Using-Recursion | class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
self.result = []
def PostOrder(node):
if node == None:
return None
PostOrder(node.left)
PostOrder(node.right)
self.result.append(node.val)
PostOrder(root)
return self.result | binary-tree-postorder-traversal | [ Python ] ✔✔ Simple Python Solution Using Recursion 🔥✌ | ASHOK_KUMAR_MEGHVANSHI | 0 | 74 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,124 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/1790591/Faster-then-64-of-speed | class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
result = []
def dfs(root,result):
if root != None:
dfs(root.left,result)
dfs(root.right,result)
result.append(root.val)
return result
... | binary-tree-postorder-traversal | Faster then 64% of speed | xevb | 0 | 19 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,125 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/1743949/textbook-solution-actually-the-definition-why-it-is-post-order | class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
res = []
def helper(root, res):
if not root:
return
helper(root.left, res)
helper(root.right, res)
res.append(root.val)
helper(root, res)
... | binary-tree-postorder-traversal | textbook solution, actually the definition why it is post-order | 752937603 | 0 | 49 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,126 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/1508475/Python3-simple-recursive-approach | class Solution:
def __init__(self):
self.ans = []
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return
if root.left:
self.postorderTraversal(root.left)
if root.right:
self.postorderTraversal(root.right)
... | binary-tree-postorder-traversal | Python3 simple recursive approach | EklavyaJoshi | 0 | 42 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,127 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/1398447/Java-%2B-Python3 | class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
res = []
if not root:
return res
def dfs(node):
if not node:
return
dfs(node.left)
dfs(node.right)
res.append(node.val)
... | binary-tree-postorder-traversal | Java + Python3 | jlee9077 | 0 | 72 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,128 |
https://leetcode.com/problems/lru-cache/discuss/442751/Python3-hashmap-and-doubly-linked-list | class ListNode:
def __init__(self, key=0, val=0, prev=None, next=None):
self.key = key
self.val = val
self.prev = prev
self.next = next
class LRUCache:
def __init__(self, capacity: int):
"""Initialize hash table & dll"""
self.cpty = capacity
... | lru-cache | [Python3] hashmap & doubly-linked list | ye15 | 8 | 457 | lru cache | 146 | 0.405 | Medium | 2,129 |
https://leetcode.com/problems/lru-cache/discuss/442751/Python3-hashmap-and-doubly-linked-list | class LRUCache:
def __init__(self, capacity: int):
self.cpty = capacity
self.data = dict()
def get(self, key: int) -> int:
if key not in self.data: return -1
value = self.data.pop(key)
self.data[key] = value
return value
def put(self, key: i... | lru-cache | [Python3] hashmap & doubly-linked list | ye15 | 8 | 457 | lru cache | 146 | 0.405 | Medium | 2,130 |
https://leetcode.com/problems/lru-cache/discuss/442751/Python3-hashmap-and-doubly-linked-list | class LRUCache:
def __init__(self, capacity: int):
self.cpty = capacity
self.data = OrderedDict()
def get(self, key: int) -> int:
if key not in self.data: return -1
self.data.move_to_end(key)
return self.data[key]
def put(self, key: int, value: int) -> None... | lru-cache | [Python3] hashmap & doubly-linked list | ye15 | 8 | 457 | lru cache | 146 | 0.405 | Medium | 2,131 |
https://leetcode.com/problems/lru-cache/discuss/1677936/Python-Dict-Only | class LRUCache:
def __init__(self, capacity: int):
self.cache = {}
self.capacity = capacity
self.items = 0
def get(self, key: int) -> int:
if key in self.cache:
val = self.cache[key] # get value of key and delete the key - value
del self.cache[k... | lru-cache | Python Dict Only | Kenchir | 7 | 338 | lru cache | 146 | 0.405 | Medium | 2,132 |
https://leetcode.com/problems/lru-cache/discuss/1374660/Python-Guys-NextIter | class LRUCache:
def __init__(self, capacity: int):
self.return1={}
self.capacity=capacity
def get(self, key: int) -> int:
if key in self.return1:
get = self.return1[key]
del self.return1[key]
self.return1[key]=get
return get
retu... | lru-cache | Python Guys #NextIter | peanuts092 | 6 | 697 | lru cache | 146 | 0.405 | Medium | 2,133 |
https://leetcode.com/problems/lru-cache/discuss/1655397/Python3-Dictionary-%2B-LinkedList-or-Clean-%2B-Detailed-Explanation-or-O(1)-Time-For-All-Operations | class Node:
def __init__(self, key=-1, val=-1, nxt=None, prv=None):
self.key, self.val, self.next, self.prev = key, val, nxt, prv
class LinkedList:
def __init__(self):
# Head is the Most Recently Used -- Tail is the Least Recently Used
self.head = self.tail = None
def appendleft(s... | lru-cache | [Python3] Dictionary + LinkedList | Clean + Detailed Explanation | O(1) Time For All Operations | PatrickOweijane | 5 | 539 | lru cache | 146 | 0.405 | Medium | 2,134 |
https://leetcode.com/problems/lru-cache/discuss/2090397/Brute-Force-greater-Optimized-Approach-O(1)-Time-Solution-Easy-To-Understand | class LRUCache:
def __init__(self, capacity):
self.capacity = capacity
self.dic = {}
self.arr = []
def get(self, key):
if key in self.arr:
i = self.arr.index(key)
res = self.arr[i]
self.arr.pop(i)
self.arr.append(res)
... | lru-cache | Brute Force => Optimized Approach O(1) Time Solution Easy To Understand✨ | samirpaul1 | 3 | 291 | lru cache | 146 | 0.405 | Medium | 2,135 |
https://leetcode.com/problems/lru-cache/discuss/2090397/Brute-Force-greater-Optimized-Approach-O(1)-Time-Solution-Easy-To-Understand | class LRUCache:
def __init__(self, capacity):
self.dic = collections.OrderedDict()
self.capacity = capacity
self.cacheLen = 0
def get(self, key):
if key in self.dic:
ans = self.dic.pop(key)
self.dic[key] = ans # set this this key as new one (mo... | lru-cache | Brute Force => Optimized Approach O(1) Time Solution Easy To Understand✨ | samirpaul1 | 3 | 291 | lru cache | 146 | 0.405 | Medium | 2,136 |
https://leetcode.com/problems/lru-cache/discuss/2090397/Brute-Force-greater-Optimized-Approach-O(1)-Time-Solution-Easy-To-Understand | class Node(object):
def __init__(self, key, x):
self.key = key
self.value = x
self.next = None
self.prev = None
class LRUCache(object):
def __init__(self, capacity):
"""
:type capacity: int
"""
self.capacity = capacity
self.dic = {... | lru-cache | Brute Force => Optimized Approach O(1) Time Solution Easy To Understand✨ | samirpaul1 | 3 | 291 | lru cache | 146 | 0.405 | Medium | 2,137 |
https://leetcode.com/problems/lru-cache/discuss/2075556/Python3-oror-faster-86.25-oror-memory-83.75 | class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.items = dict()
def get(self, key: int) -> int:
result = self.items.pop(key, -1)
if result == -1:
return -1
else:
self.items[key] = result
return ... | lru-cache | Python3 || faster 86.25% || memory 83.75% | grivabo | 3 | 171 | lru cache | 146 | 0.405 | Medium | 2,138 |
https://leetcode.com/problems/lru-cache/discuss/2656078/Python3-Dict-solution-or-No-Doubly-linked-List-or-No-OrderedDict | class LRUCache:
def __init__(self, capacity):
self.dic = {}
self.remain = capacity
def get(self, key):
if key not in self.dic:
return -1
v = self.dic.pop(key)
self.dic[key] = v # set key as the newest one
return v
def put(self, key, value):
... | lru-cache | Python3 Dict solution | No Doubly linked List | No OrderedDict | ivan_shelonik | 2 | 164 | lru cache | 146 | 0.405 | Medium | 2,139 |
https://leetcode.com/problems/lru-cache/discuss/1970603/python-3-oror-ordered-map | class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.cache = collections.OrderedDict()
def get(self, key: int) -> int:
if key in self.cache:
self.cache.move_to_end(key)
return self.cache[key]
return -1
def put(self, key: ... | lru-cache | python 3 || ordered map | dereky4 | 2 | 236 | lru cache | 146 | 0.405 | Medium | 2,140 |
https://leetcode.com/problems/lru-cache/discuss/1950628/OrderedDict-simple | class LRUCache:
def __init__(self, capacity: int):
self._ordered_dict = collections.OrderedDict()
self._capacity = capacity
def get(self, key: int) -> int:
if key in self._ordered_dict:
self._ordered_dict[key] = self._ordered_dict.pop(key)
return self._ordered_d... | lru-cache | OrderedDict simple | ismaj | 2 | 98 | lru cache | 146 | 0.405 | Medium | 2,141 |
https://leetcode.com/problems/lru-cache/discuss/2676308/Python-or-DLL-or-Hashmap-or-Beats-95-Memory | class Node:
def __init__(self, key, value):
self.key = key
self.value = value
self.next = None
self.prev = None
class LRUCache:
def __init__(self, capacity: int):
self.head = None
self.tail = None
self.hash_map = {}
self.capacity = capacity
... | lru-cache | Python | DLL | Hashmap | Beats 95% Memory | gurubalanh | 1 | 33 | lru cache | 146 | 0.405 | Medium | 2,142 |
https://leetcode.com/problems/lru-cache/discuss/2654152/Python-using-default-dict() | class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.cache = dict() # using default dict in Python
def get(self, key: int) -> int:
if key not in self.cache.keys():
return -1
data = self.cache.pop(key)
self.cache[key] = data
return data
def put(self... | lru-cache | Python答え using default dict() | namashin | 1 | 106 | lru cache | 146 | 0.405 | Medium | 2,143 |
https://leetcode.com/problems/lru-cache/discuss/2115259/Python-oror-Easy-Double-Linked-List-solution | class Node:
def __init__(self, key, val):
self.key, self.val = key, val
self.prev = self.next = None
class LRUCache:
def __init__(self, capacity: int):
self.cap = capacity
self.cache = {}
# left helps to find LRU and right helps to find MRU
self.left, self.right = Node(0, 0), Node(0, 0)
self.left.n... | lru-cache | Python || Easy Double Linked List solution | sagarhasan273 | 1 | 125 | lru cache | 146 | 0.405 | Medium | 2,144 |
https://leetcode.com/problems/lru-cache/discuss/1959893/Easy-to-Understand-Python-code-that-beats-90-solutions. | class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.dic = dict()
self.head = Node(0, 0)
self.tail = Node(0, 0)
self.head.next = self.tail
self.tail.prev = self.head
def get(self, key: int) -> int:
if key in self.dic:
... | lru-cache | Easy to Understand Python code that beats 90% solutions. | tkdhimanshusingh | 1 | 194 | lru cache | 146 | 0.405 | Medium | 2,145 |
https://leetcode.com/problems/lru-cache/discuss/1766553/Simple-Python3-solution-using-dict-beats-89 | class LRUCache:
"""
1. Python's dictionaries support insertion order by default.
2. You can grab access to the first key in a dictionary using either next(iter(dict)) or
list(dict[0])
"""
def __init__(self, capacity: int):
self.cache = dict()
self.capacity = capacity
... | lru-cache | Simple Python3 solution using dict beats 89% | kunalnarang | 1 | 237 | lru cache | 146 | 0.405 | Medium | 2,146 |
https://leetcode.com/problems/lru-cache/discuss/1707041/Python-Solution | class Node:
def __init__(s, key, value):
s.key = key
s.value = value
s.next = None
s.prev = None
class LRUCache:
def __init__(self, capacity: int):
self.mru = Node(-1, -1)
self.lru = Node(-1, -1)
self.mru.next = self.lru
self.lru.prev = self.mru
self.hash = {}
self.size = capacity
def ... | lru-cache | Python Solution | ayush_kushwaha | 1 | 191 | lru cache | 146 | 0.405 | Medium | 2,147 |
https://leetcode.com/problems/lru-cache/discuss/554251/Python3-O(1)-solution-using-doubly-linked-list | class LRUCache:
class Node:
'''
A class that represents a cache-access node in the doubly-linked list
'''
def __init__(self, key, value, prev_n = None, next_n = None):
self.key = key
self.value = value
self.prev_n = prev_n
self.next_n... | lru-cache | Python3 O(1) solution using doubly-linked list | coolhazcker | 1 | 220 | lru cache | 146 | 0.405 | Medium | 2,148 |
https://leetcode.com/problems/lru-cache/discuss/517055/Clear-Logic-Details-Explained-Python-beats-93 | class Node:
def __init__(self, key, val):
self.val = val
self.key = key
self.prev = None
self.nxt_ = None
# use a DL
class DLinked:
def __init__(self):
self.head = None
self.tail = None
def remove(self, node):
""" return the deleted node key
... | lru-cache | Clear Logic, Details Explained, Python beats 93% | xia50 | 1 | 144 | lru cache | 146 | 0.405 | Medium | 2,149 |
https://leetcode.com/problems/lru-cache/discuss/469599/Python3-O(n)-time-complexity-using-a-list()-and-a-hash-table | class LRUCache:
def __init__(self, capacity: int):
self.capacity=capacity
self.frequency = []
self.cache = {}
def get(self, key: int) -> int:
if key not in self.frequency:
return -1
index = self.frequency.index(key)
del self.frequency[index]
self.frequency.append(key)
return self.cache[key]
de... | lru-cache | Python3 O(n) time complexity using a list() and a hash table | jb07 | 1 | 269 | lru cache | 146 | 0.405 | Medium | 2,150 |
https://leetcode.com/problems/lru-cache/discuss/2845990/Using-Pythons-ordered-dict-property | class LRUCache:
def __init__(self, capacity: int):
self.orderedDict = {}
self.cap = capacity
def updateToTop(self, key):
v = self.orderedDict[key]
del self.orderedDict[key]
self.orderedDict[key] = v
def get(self, key: int) -> int:
if key in self.orderedDict... | lru-cache | Using Pythons ordered dict property | Jacomatt | 0 | 4 | lru cache | 146 | 0.405 | Medium | 2,151 |
https://leetcode.com/problems/lru-cache/discuss/2839753/Python-solution-with-no-Ordered-Dict-nor-linked-list | class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.cache = {}
def get(self, key: int) -> int:
if key in self.cache:
v = self.cache.pop(key)
self.cache[key] = v
return v
else:
return -1
def put(se... | lru-cache | Python solution with no Ordered Dict nor linked list | woaying | 0 | 2 | lru cache | 146 | 0.405 | Medium | 2,152 |
https://leetcode.com/problems/lru-cache/discuss/2838553/Hashmap-%2B-Doubly-LinkedList | class Node:
def __init__(self, key: Optional[int] = None, value: Optional[int] = None) -> None:
self.key = key
self.val = value
self.next = None
self.prev = None
class DoublyLinkedList:
def __init__(self) -> None:
self.head = Node() # dummy head. The real head actually ... | lru-cache | Hashmap + Doubly LinkedList | fengdi2020 | 0 | 5 | lru cache | 146 | 0.405 | Medium | 2,153 |
https://leetcode.com/problems/lru-cache/discuss/2836402/PYTHON3-EASY-EXPLANATION-ONLY-1-DICTor-99.71 | class LRUCache:
def __init__(self, capacity: int):
self.cap, self.cache = capacity, {}
def get(self, key: int) -> int:
if key in self.cache:
temp = self.cache[key]
del self.cache[key]
self.cache[key] = temp
return self.cache[key]
... | lru-cache | [PYTHON3] EASY EXPLANATION 🔥 ONLY 1 DICT| 99.71% | papaggalos | 0 | 3 | lru cache | 146 | 0.405 | Medium | 2,154 |
https://leetcode.com/problems/lru-cache/discuss/2834061/Python-Use-a-single-dict-for-keyvalues-and-doubly-linked-list | class LRUCache:
def __init__(self, capacity: int):
self.n = capacity
self.byKey = {}
self.head = None
self.tail = None
def extract(self, key, new):
if new[1] is not None:
self.byKey[new[1]][2] = new[2]
if new[2] is not None:
self.byKey[new... | lru-cache | [Python] Use a single dict for key/values and doubly linked list | constantstranger | 0 | 2 | lru cache | 146 | 0.405 | Medium | 2,155 |
https://leetcode.com/problems/lru-cache/discuss/2832485/Python3-98-faster-with-explanation | class LRUCache:
def __init__(self, capacity: int):
self.cap = capacity
self.imap = {}
def get(self, key: int) -> int:
if key in self.imap:
tmp = self.imap[key]
del self.imap[key]
self.imap[key] = tmp
return self.... | lru-cache | Python3, 98% faster with explanation | cvelazquez322 | 0 | 6 | lru cache | 146 | 0.405 | Medium | 2,156 |
https://leetcode.com/problems/lru-cache/discuss/2830129/Python3.7%2B-or-No-OrderedDict-Short-Python-Solution-using-DictionaryHashmap | class LRUCache:
def __init__(self, capacity: int):
self.d = {}
self.size = capacity
def get(self, key: int) -> int:
if key not in self.d: return -1
self.d[key] = self.d.pop(key)
return self.d[key]
def put(self, key: int, value: int) -> None:
if key in self.... | lru-cache | Python3.7+ | No OrderedDict Short Python Solution using Dictionary/Hashmap | Aleph-Null | 0 | 1 | lru cache | 146 | 0.405 | Medium | 2,157 |
https://leetcode.com/problems/lru-cache/discuss/2810727/Python-or-Simple-or-HashTable-or-LinkedList-or-Fast | class Node:
def __init__(self, key = -1, val = -1):
self.key, self.val = key, val
self.prev, self.next = None, None
class LRUCache:
def __init__(self, capacity: int):
self.size = capacity
self.cache = {}
self.head, self.tail = Node(), Node()
self.head.next, sel... | lru-cache | Python | Simple | HashTable | LinkedList | Fast | david-cobbina | 0 | 5 | lru cache | 146 | 0.405 | Medium | 2,158 |
https://leetcode.com/problems/lru-cache/discuss/2759827/Python-orderedict | class LRUCache:
def __init__(self, capacity: int):
self.cache = collections.OrderedDict()
self.capacity = capacity
def get(self, key: int) -> int:
if key not in self.cache:
return -1
value = self.cache.get(key)
self.cache.move_to_end(key)
re... | lru-cache | Python orderedict | ychhhen | 0 | 7 | lru cache | 146 | 0.405 | Medium | 2,159 |
https://leetcode.com/problems/lru-cache/discuss/2756623/Python%3A-dict()%2BDoubly-linkedList | class node:
def __init__(self, key: int, val: int, pre = None, nxt = None):
self.key = key
self.val = val
self.pre = pre
self.nxt = nxt
class LRUCache:
def __init__(self, capacity: int):
self.key2val = dict()
self.MAXLEN = capacity
self.size = 0
... | lru-cache | Python: dict()+Doubly-linkedList | KKCrush | 0 | 4 | lru cache | 146 | 0.405 | Medium | 2,160 |
https://leetcode.com/problems/lru-cache/discuss/2756622/Python%3A-dict()%2BDoubly-linkedList | class node:
def __init__(self, key: int, val: int, pre = None, nxt = None):
self.key = key
self.val = val
self.pre = pre
self.nxt = nxt
class LRUCache:
def __init__(self, capacity: int):
self.key2val = dict()
self.MAXLEN = capacity
self.size = 0
... | lru-cache | Python: dict()+Doubly-linkedList | KKCrush | 0 | 3 | lru cache | 146 | 0.405 | Medium | 2,161 |
https://leetcode.com/problems/lru-cache/discuss/2690470/LRU-Cache | class ListNode:
def __init__(self, key = 0, val = 0):
self.key = key
self.val = val
self.prev = None
self.next = None
class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.hashmap = {}
self.head = ListNode()
self.tail = ... | lru-cache | LRU Cache | Erika_v | 0 | 7 | lru cache | 146 | 0.405 | Medium | 2,162 |
https://leetcode.com/problems/lru-cache/discuss/2669692/Python-or-Easy-way-or-Ordered-Dictionary | class LRUCache:
def __init__(self, capacity: int):
self.lru_cache = collections.OrderedDict()
self.capacity = capacity
def get(self, key: int) -> int:
if key not in self.lru_cache: return -1
self.lru_cache.move_to_end(key)
return self.lru_cache[key]
def put(self, ... | lru-cache | Python | Easy way | Ordered Dictionary | gurubalanh | 0 | 10 | lru cache | 146 | 0.405 | Medium | 2,163 |
https://leetcode.com/problems/lru-cache/discuss/2636368/LRU-or-three-solutions | class LinkedNode:
def __init__(self, key = None, value = None, next = None):
self.key = key
self.value = value
self.next = next
class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.dummy = LinkedNode()
self.tail = self.dummy
... | lru-cache | LRU | three solutions | MichelleZou | 0 | 50 | lru cache | 146 | 0.405 | Medium | 2,164 |
https://leetcode.com/problems/lru-cache/discuss/2626540/Python-simple-solution-with-DoubleLinkedList | class Node:
def __init__(self, key, value):
self.key = key
self.value = value
self.prev = None
self.next = None
class LRUCache:
def __init__(self, capacity: int):
self.cap = capacity
self.cache = {} #key : key, value: node
self.head, self.tail = ... | lru-cache | Python simple solution with DoubleLinkedList | ljxowen | 0 | 57 | lru cache | 146 | 0.405 | Medium | 2,165 |
https://leetcode.com/problems/lru-cache/discuss/2611952/Python-EasyUnderstanding-Solution | class Node:
def __init__(self,key=0,value=0):
self.key,self.value = key,value
self.prev = self.next = None
class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.cache = {}
self.left,self.right = Node(),Node()
self.rig... | lru-cache | Python EasyUnderstanding Solution | al5861 | 0 | 59 | lru cache | 146 | 0.405 | Medium | 2,166 |
https://leetcode.com/problems/lru-cache/discuss/2601550/Extremely-Simple-Python-solution-with-Explanation | class LRUCache:
# Easy peasy timer based solution
def __init__(self, capacity: int):
# Cache for value storage of a key
# lru for lru priority storage of the key
# Can a priority queue minheap also work? I think it will i just need to figure out how!
self.cache = {}
self.... | lru-cache | Extremely Simple Python solution with Explanation | shiv-codes | 0 | 89 | lru cache | 146 | 0.405 | Medium | 2,167 |
https://leetcode.com/problems/lru-cache/discuss/2516850/146.-My-PythonandJAVA-Solution-with-comments | class Node:
def __init__(self, key, val):
self.key = key
self.val = val
self.prev = None
self.next = None
class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.cache = dict()
# elem in dict are pair [key, node]
... | lru-cache | 146. My Python&JAVA Solution with comments | JunyiLin | 0 | 23 | lru cache | 146 | 0.405 | Medium | 2,168 |
https://leetcode.com/problems/lru-cache/discuss/2516850/146.-My-PythonandJAVA-Solution-with-comments | class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.cache = collections.OrderedDict()
# we use collecitons toolbox
# orderedDict is a dictionary that will remember the order that each (key,value) pair that comes in
# we also have function... | lru-cache | 146. My Python&JAVA Solution with comments | JunyiLin | 0 | 23 | lru cache | 146 | 0.405 | Medium | 2,169 |
https://leetcode.com/problems/lru-cache/discuss/2491820/Python-runtime-42.97-memory-11.54 | class LinkedList:
def __init__(self, val):
self.val = val
self.next = None
self.prev = None
class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.d = {}
self.head = None
self.end = None
def get(self, key: int) -> int:
... | lru-cache | Python, runtime 42.97%, memory 11.54% | tsai00150 | 0 | 59 | lru cache | 146 | 0.405 | Medium | 2,170 |
https://leetcode.com/problems/lru-cache/discuss/2491820/Python-runtime-42.97-memory-11.54 | class LinkedList:
def __init__(self, key, val):
self.key = key
self.val = val
self.next = None
self.prev = None
class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.d = {}
self.head = None
self.head = LinkedList(0,0)
... | lru-cache | Python, runtime 42.97%, memory 11.54% | tsai00150 | 0 | 59 | lru cache | 146 | 0.405 | Medium | 2,171 |
https://leetcode.com/problems/lru-cache/discuss/2437043/Python-Hashtable | class Node:
def __init__(self, key: int , val: int):
self.key, self.val = key, val
self.prev = self.next = None
class LRUCache:
def __init__(self, capacity: int):
self.cap = capacity
self.cache = {} # key : node
# init double linked list
self.he... | lru-cache | Python Hashtable | cccandj | 0 | 70 | lru cache | 146 | 0.405 | Medium | 2,172 |
https://leetcode.com/problems/lru-cache/discuss/2431064/Python-Easy-to-Understand-oror-O(1)-Time-Complexity | class Node:
def __init__(self,key, value):
self.key, self.value = key, value
self.next, self.prev = None, None
class LRUCache:
def __init__(self, capacity: int):
self.cap = capacity
self.cache = {}
self.left = self.right = Node(0, 0)
self.left.next, self.right.p... | lru-cache | Python - Easy to Understand || O(1) Time Complexity | dayaniravi123 | 0 | 75 | lru cache | 146 | 0.405 | Medium | 2,173 |
https://leetcode.com/problems/lru-cache/discuss/2238187/Python3-oror-List-oror-Simple-and-Easy-Approach-oror-Explained | class LRUCache:
def __init__(self, capacity: int):
self.lru = {}
self.capacity = capacity
def get(self, key: int) -> int:
# check if key exists in lru
if key not in self.lru:
return -1
# repriotize the cache with the input key, which means bring th... | lru-cache | Python3 || List || Simple and Easy Approach || Explained | mrusmanshahid | 0 | 82 | lru cache | 146 | 0.405 | Medium | 2,174 |
https://leetcode.com/problems/lru-cache/discuss/2133141/Clear-Linked-Hashmap-solution-in-Python | class ListNode:
def __init__(self, key, val):
self.key = key
self.val = val
self.next = None
self.prev = None
class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
# a hashmap stores key to ListNode(key, value) pair
self.map = dict()
... | lru-cache | Clear Linked Hashmap solution in Python | leqinancy | 0 | 30 | lru cache | 146 | 0.405 | Medium | 2,175 |
https://leetcode.com/problems/insertion-sort-list/discuss/1176552/Python3-188ms-Solution-(explanation-with-visualization) | class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
# No need to sort for empty list or list of size 1
if not head or not head.next:
return head
# Use dummy_head will help us to handle insertion before head easily
dummy_head = ListNo... | insertion-sort-list | [Python3] 188ms Solution (explanation with visualization) | EckoTan0804 | 28 | 834 | insertion sort list | 147 | 0.503 | Medium | 2,176 |
https://leetcode.com/problems/insertion-sort-list/discuss/1629375/Python3-INSERTION-SORT-Explained | class Solution:
def insertionSortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
sort = ListNode() #dummy node
cur = head
while cur:
sortCur = sort
while sortCur.next and cur.val >= sortCur.next.val:
sortCur = sortCur.next
... | insertion-sort-list | ✔️ [Python3] INSERTION SORT, Explained | artod | 7 | 1,700 | insertion sort list | 147 | 0.503 | Medium | 2,177 |
https://leetcode.com/problems/insertion-sort-list/discuss/920840/Python-Solution-Explained-(video-%2B-code) | class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
dummy_head = ListNode()
curr = head
while curr:
prev_pointer = dummy_head
next_pointer = dummy_head.next
while next_pointer:
if curr.val < next_p... | insertion-sort-list | Python Solution Explained (video + code) | spec_he123 | 5 | 295 | insertion sort list | 147 | 0.503 | Medium | 2,178 |
https://leetcode.com/problems/insertion-sort-list/discuss/2474623/Python-Easiest-Insertion-sort | class Solution:
def insertionSortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(0,head)
prev, curr = head,head.next
# we can't go back so keep a previous pointer
while curr:
if curr.val >= prev.val:
prev = curr
... | insertion-sort-list | Python Easiest Insertion sort | Abhi_009 | 1 | 103 | insertion sort list | 147 | 0.503 | Medium | 2,179 |
https://leetcode.com/problems/insertion-sort-list/discuss/1630153/Python3-O(1)-space-solution-with-comments | class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
dummy = ListNode(0, next=head)
cur = head
while cur.next:
# beginning of the list
begin = dummy
#traverse every element from begin of the list while not found e... | insertion-sort-list | [Python3] O(1) space solution with comments | maosipov11 | 1 | 79 | insertion sort list | 147 | 0.503 | Medium | 2,180 |
https://leetcode.com/problems/insertion-sort-list/discuss/2813143/python-fast-sol.-faster-then-98.95 | class Solution:
def insertionSortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
nums = []
cur = ans = head
while head:
nums.append(head.val)
head = head.next
nums = sorted(nums)
count = 0
while cur:
... | insertion-sort-list | python fast sol. faster then 98.95 % | pranjalmishra334 | 0 | 6 | insertion sort list | 147 | 0.503 | Medium | 2,181 |
https://leetcode.com/problems/insertion-sort-list/discuss/2801894/Python-solution | class Solution:
def insertionSortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(float('inf'))
dummy.next = None
lists = [dummy]
while head:
for i in range(len(lists)):
if head.val < lists[i].val:
l... | insertion-sort-list | Python solution | maomao1010 | 0 | 1 | insertion sort list | 147 | 0.503 | Medium | 2,182 |
https://leetcode.com/problems/insertion-sort-list/discuss/2605607/Mixture-of-bubble-and-insertion-sort-Python | class Solution:
# Mixture of bubble and insertion sort
def insertionSortList(self, head: ListNode) -> ListNode:
dummy = ListNode(0, head)
prev, curr = head, head.next
while curr:
if prev.val <= curr.val:
prev, curr = prev.next, curr.next
... | insertion-sort-list | Mixture of bubble and insertion sort Python | shiv-codes | 0 | 22 | insertion sort list | 147 | 0.503 | Medium | 2,183 |
https://leetcode.com/problems/insertion-sort-list/discuss/2518977/Python-Clearly-commented-insertion-sort-Sentinel-Node | class Solution:
def insertionSortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
# keep the start of the list -> sentinel node
# this really helps us to keep the code concise
sentinel = ListNode(val=-50001, next=head)
# we need to iterate through the lis... | insertion-sort-list | [Python] - Clearly commented insertion sort - Sentinel Node | Lucew | 0 | 69 | insertion sort list | 147 | 0.503 | Medium | 2,184 |
https://leetcode.com/problems/insertion-sort-list/discuss/1996065/python-3-oror-simple-iterative-solution-oror-O(n2)O(1) | class Solution:
def insertionSortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(0, head)
prevI, i = dummy, head
while i:
prevJ, j = dummy, dummy.next
while j.val < i.val:
prevJ, j = j, j.next
if i is j:
... | insertion-sort-list | python 3 || simple iterative solution || O(n^2)/O(1) | dereky4 | 0 | 168 | insertion sort list | 147 | 0.503 | Medium | 2,185 |
https://leetcode.com/problems/insertion-sort-list/discuss/1963960/Naive-approach-with-99.10-efficiency | class Solution:
def insertionSortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
temp=head
l=[]
while temp:
l.append(temp.val)
temp=temp.next
l.sort()
d=ListNode(-1)
res=d
for i in l:
res.next=ListNode(i)
... | insertion-sort-list | Naive approach with 99.10% efficiency | captain_sathvik | 0 | 78 | insertion sort list | 147 | 0.503 | Medium | 2,186 |
https://leetcode.com/problems/insertion-sort-list/discuss/1630404/Python3-Easy-to-follow-up-solution | class Solution:
def insertionSortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
last_sorted = head
it = last_sorted.next
temp = ListNode(val=-1, next=last_sorted)
while it is not None:
if last_sorted.val <= it.val:
last_sorted = it
... | insertion-sort-list | [Python3] Easy to follow up solution | varian97 | 0 | 29 | insertion sort list | 147 | 0.503 | Medium | 2,187 |
https://leetcode.com/problems/insertion-sort-list/discuss/1629650/Python3-Insertion-Sort-or-Inplace-or-O(1)-memory-or-98.51-Memory-Usage-or-O(n2)-Time-complexity | class Solution:
def insertionSortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
temp=head
#Iterate over list
while(temp):
#store next value of current node
n=temp.next
# To avoid circular loop
if(temp==head):
prev=temp
... | insertion-sort-list | [Python3] Insertion Sort | Inplace | O(1) memory | 98.51% Memory Usage | O(n^2) Time complexity | Cdeo05 | 0 | 79 | insertion sort list | 147 | 0.503 | Medium | 2,188 |
https://leetcode.com/problems/insertion-sort-list/discuss/1072564/python-3-solution-O(N*N) | class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
if head==None:
return head
h=head
prev=head
head=head.next
while(head):
node=h
while(node!=head):
if node.val>head.val and node==h:
... | insertion-sort-list | python 3 solution O(N*N) | AchalGupta | 0 | 119 | insertion sort list | 147 | 0.503 | Medium | 2,189 |
https://leetcode.com/problems/insertion-sort-list/discuss/921121/Python3-solution | class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
if not head: return head
e = head # end of sorted portion
n = e.next # next node to sort
while n: # if next node exists
s = head
p = None
while s != n: #scan from head to thru... | insertion-sort-list | Python3 solution | dalechoi | 0 | 208 | insertion sort list | 147 | 0.503 | Medium | 2,190 |
https://leetcode.com/problems/insertion-sort-list/discuss/920733/insertionSortList-or-python3-intuitive-two-pointer | class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
if not head: return None
hi, hp = head.next, head
while hi:
lo, lp = head, None
while lo:
if lo == hi:
hp, hi = hi, hi.next
break
... | insertion-sort-list | insertionSortList | python3 intuitive two pointer | hangyu1130 | 0 | 62 | insertion sort list | 147 | 0.503 | Medium | 2,191 |
https://leetcode.com/problems/insertion-sort-list/discuss/715153/Python3-O(N2)-algo | class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
node, prev = head, None # ...<-###<-prev, node->###->...
while node:
if not prev or prev.val <= node.val:
node.next, node, prev = prev, node.next, node #set new head of reversed list & move pre... | insertion-sort-list | [Python3] O(N^2) algo | ye15 | 0 | 60 | insertion sort list | 147 | 0.503 | Medium | 2,192 |
https://leetcode.com/problems/insertion-sort-list/discuss/715153/Python3-O(N2)-algo | class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
#collect values
nums = []
node = head
while node:
nums.append(node.val)
node = node.next
#sort
nums.sort()
#reconstruct linked list
d... | insertion-sort-list | [Python3] O(N^2) algo | ye15 | 0 | 60 | insertion sort list | 147 | 0.503 | Medium | 2,193 |
https://leetcode.com/problems/insertion-sort-list/discuss/715153/Python3-O(N2)-algo | class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
dummy = node = ListNode(next=head) # dummy head
while (temp := node.next):
prev = dummy
while prev.next.val < temp.val: prev = prev.next
if prev.next != temp:
node.next = ... | insertion-sort-list | [Python3] O(N^2) algo | ye15 | 0 | 60 | insertion sort list | 147 | 0.503 | Medium | 2,194 |
https://leetcode.com/problems/insertion-sort-list/discuss/715153/Python3-O(N2)-algo | class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
dummy = node = ListNode(val=-inf, next=head)
while node.next:
if node.val <= node.next.val: node = node.next
else:
temp = node.next
prev = dummy
while... | insertion-sort-list | [Python3] O(N^2) algo | ye15 | 0 | 60 | insertion sort list | 147 | 0.503 | Medium | 2,195 |
https://leetcode.com/problems/insertion-sort-list/discuss/1176570/Python3-44ms-(beats-98.25)-by-using-Python-sorted()-function | class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
# No need to sort for empty list or list of size 1
if not head or not head.next:
return head
nodes = []
cur = head
while head:
nodes.append(head)
hea... | insertion-sort-list | [Python3] 44ms (beats 98.25%) by using Python sorted() function | EckoTan0804 | -5 | 227 | insertion sort list | 147 | 0.503 | Medium | 2,196 |
https://leetcode.com/problems/sort-list/discuss/1796085/Sort-List-or-Python-O(nlogn)-Solution-or-95-Faster | class Solution:
def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head or not head.next:
return head
# Split the list into two halfs
left = head
right = self.getMid(head)
tmp = right.next
right.next = None
right = ... | sort-list | ✔️ Sort List | Python O(nlogn) Solution | 95% Faster | pniraj657 | 25 | 1,900 | sort list | 148 | 0.543 | Medium | 2,197 |
https://leetcode.com/problems/sort-list/discuss/1795826/Python-Simple-Python-Solution-With-Two-Approach | class Solution:
def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
new_node=head
result = new_node
array = []
while head != None:
array.append(head.val)
head=head.next
array = sorted(array)
for num in array:
new_node.val = num
new_node = new_node.next
return result | sort-list | [ Python ] ✔✔ Simple Python Solution With Two Approach 🔥✌ | ASHOK_KUMAR_MEGHVANSHI | 8 | 315 | sort list | 148 | 0.543 | Medium | 2,198 |
https://leetcode.com/problems/sort-list/discuss/1795136/Python3-Clean%2BSimple-or-FastandSlow-or-Mergesort | class Solution:
def split(self, head):
prev = None
slow = fast = head
while fast and fast.next:
prev = slow
slow = slow.next
fast = fast.next.next
if prev: prev.next = None
return slow
def merge(self, l1, l2):
dummy = tail ... | sort-list | [Python3] Clean+Simple | Fast&Slow | Mergesort | PatrickOweijane | 5 | 720 | sort list | 148 | 0.543 | Medium | 2,199 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.