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/linked-list-cycle/discuss/1457013/Python3C%2B%2B-Several-Solutions-and-O(1)-space | class Solution:
def hasCycle(self, head: ListNode) -> bool:
d = {}
cur = head
while cur:
if cur in d:
return True
d[cur] = 1
cur = cur.next
return False | linked-list-cycle | [Python3/C++] Several Solutions and O(1) space | light_1 | 1 | 142 | linked list cycle | 141 | 0.47 | Easy | 2,000 |
https://leetcode.com/problems/linked-list-cycle/discuss/1457013/Python3C%2B%2B-Several-Solutions-and-O(1)-space | class Solution:
def hasCycle(self, head: ListNode) -> bool:
cur = head
try:
slow_p = cur.next
fast_p = cur.next.next
except:
return False
while cur:
if slow_p == fast_p:
return True
t... | linked-list-cycle | [Python3/C++] Several Solutions and O(1) space | light_1 | 1 | 142 | linked list cycle | 141 | 0.47 | Easy | 2,001 |
https://leetcode.com/problems/linked-list-cycle/discuss/1457013/Python3C%2B%2B-Several-Solutions-and-O(1)-space | class Solution:
def hasCycle(self, head: ListNode) -> bool:
slow_p = head
fast_p = head
while(slow_p and fast_p and fast_p.next):
slow_p = slow_p.next
fast_p = fast_p.next.next
if slow_p == fast_p:
return True
return... | linked-list-cycle | [Python3/C++] Several Solutions and O(1) space | light_1 | 1 | 142 | linked list cycle | 141 | 0.47 | Easy | 2,002 |
https://leetcode.com/problems/linked-list-cycle/discuss/1405891/Java-%2B-Python | class Solution:
def hasCycle(self, head: ListNode) -> bool:
if not head or not head.next: return
slow, fast = head, head.next
while fast and fast.next:
if slow == fast:
return True
slow = slow.next
fast = fast.next.next | linked-list-cycle | Java + Python | jlee9077 | 1 | 72 | linked list cycle | 141 | 0.47 | Easy | 2,003 |
https://leetcode.com/problems/linked-list-cycle/discuss/1387084/Python3-Slow-Pointer-Fast-Pointer-O(1)-Space-O(N)-Time | class Solution:
def hasCycle(self, head: ListNode) -> bool:
slow = head
fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False | linked-list-cycle | [Python3] Slow Pointer Fast Pointer O(1) Space O(N) Time | whitehatbuds | 1 | 198 | linked list cycle | 141 | 0.47 | Easy | 2,004 |
https://leetcode.com/problems/linked-list-cycle/discuss/1331497/A-different-approach-(-different-from-turtles-and-hare) | class Solution:
def hasCycle(self, head: ListNode) -> bool:
if head:
while head.next:
# checking if we encounter the unique value again
if head.val == 10000000:
return True
else:
# setting the value to something unique
... | linked-list-cycle | A different approach ( different from turtles and hare) | thedarkace | 1 | 31 | linked list cycle | 141 | 0.47 | Easy | 2,005 |
https://leetcode.com/problems/linked-list-cycle/discuss/1047665/PythonC%2B%2BJava-Simple-Solution | class Solution:
def hasCycle(self, head: ListNode) -> bool:
slow=fast=head
while fast and fast.next:
slow=slow.next
fast=fast.next.next
if slow==fast:
return 1
return 0 | linked-list-cycle | [Python/C++/Java] Simple Solution | lokeshsenthilkumar | 1 | 55 | linked list cycle | 141 | 0.47 | Easy | 2,006 |
https://leetcode.com/problems/linked-list-cycle/discuss/637511/JavaPython3-Floyd's-tortoise-and-hare-algo | class Solution:
def hasCycle(self, head: ListNode) -> bool:
fast = slow = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if fast == slow: return True
return False | linked-list-cycle | [Java/Python3] Floyd's tortoise and hare algo | ye15 | 1 | 98 | linked list cycle | 141 | 0.47 | Easy | 2,007 |
https://leetcode.com/problems/linked-list-cycle/discuss/2848371/Simple-Python-Solution | class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
marker_one=head
marker_two=head
while marker_two != None and marker_two.next != None:
marker_one = marker_one.next
marker_two = marker_two.next.next
if marker_two == marker_... | linked-list-cycle | Simple Python Solution | MohammadAreeb | 0 | 1 | linked list cycle | 141 | 0.47 | Easy | 2,008 |
https://leetcode.com/problems/linked-list-cycle/discuss/2828826/python-oror-simple-solution-oror-slow-fast-pointers | class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
# if linkedlist empty
if not head:
return False
# fast, slow nodes
fast = slow = head
# counter for fast and slow
cnt = 0
# while before end of linkedl... | linked-list-cycle | python || simple solution || slow-fast pointers | wduf | 0 | 6 | linked list cycle | 141 | 0.47 | Easy | 2,009 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2184711/O(1)-Space-Python-solution-with-clear-explanation-faster-than-90-solutions | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
fast, slow = head, head
while(fast and fast.next):
fast = fast.next.next
slow = slow.next
if(fast == slow):
slow = head
while(slow is not fast):
... | linked-list-cycle-ii | O(1) Space Python solution with clear explanation faster than 90% solutions | saiamrit | 30 | 844 | linked list cycle ii | 142 | 0.465 | Medium | 2,010 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2688845/Python-2-Easy-Way-To-Solve-or-99-Faster-or-Fast-and-Simple-Solution | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
fast ,slow = head ,head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
slow = head
while fast != slow:
... | linked-list-cycle-ii | ✔️ Python 2 Easy Way To Solve | 99% Faster | Fast and Simple Solution | pniraj657 | 2 | 220 | linked list cycle ii | 142 | 0.465 | Medium | 2,011 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2688845/Python-2-Easy-Way-To-Solve-or-99-Faster-or-Fast-and-Simple-Solution | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
lookup = set()
while head:
if head in lookup:
return head
lookup.add(head)
head = head.next
return None | linked-list-cycle-ii | ✔️ Python 2 Easy Way To Solve | 99% Faster | Fast and Simple Solution | pniraj657 | 2 | 220 | linked list cycle ii | 142 | 0.465 | Medium | 2,012 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/912474/Linked-List-Cycle-II-or-python3-Floyd's-cycle-finding-algorithm-constant-space | class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
lo = hi = head
while hi and hi.next:
lo = lo.next
hi = hi.next.next
if lo == hi:
break
else:
return None
lo = head
while lo != hi:
lo... | linked-list-cycle-ii | Linked List Cycle II | python3 Floyd's cycle-finding algorithm constant space | hangyu1130 | 2 | 153 | linked list cycle ii | 142 | 0.465 | Medium | 2,013 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2412366/Python-97.5-Faster-or-Easy-Explanation | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return None
if head.next == head or head.next == None:
return head.next
curr = head
visited = set()
while curr:
visited.ad... | linked-list-cycle-ii | Python 97.5 % Faster | Easy Explanation | shahidmu | 1 | 128 | linked list cycle ii | 142 | 0.465 | Medium | 2,014 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2288028/Python3-two-pointer-approach | class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
if head==None or head.next==None:
return None
slow = head
fast = head
flag = 0
while slow and fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow=... | linked-list-cycle-ii | 📌 Python3 two pointer approach | Dark_wolf_jss | 1 | 61 | linked list cycle ii | 142 | 0.465 | Medium | 2,015 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/1701092/Python-Solution-oror-Faster-than-98.88 | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head is None:
return None
slow = head
li = set()
while slow:
li.add(slow)
slow = slow.next
if slow in li:
return slow
... | linked-list-cycle-ii | Python Solution || Faster than 98.88% | KiranUpase | 1 | 65 | linked list cycle ii | 142 | 0.465 | Medium | 2,016 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/1443570/Python3-Fast-slow-pointers-or-Three-pointers | class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
if not head or not head.next:
return None
slow = fast = entry = head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
... | linked-list-cycle-ii | Python3, Fast-slow pointers | Three pointers | leadingcoder | 1 | 96 | linked list cycle ii | 142 | 0.465 | Medium | 2,017 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/1381922/Easy-Python-Solution-(Faster-than-95) | class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
s, f = head, head
if not s:
return None
if not s.next:
return None
while f and f.next:
f, s = f.next.next, s.next
if f == s:
b... | linked-list-cycle-ii | Easy Python Solution (Faster than 95%) | the_sky_high | 1 | 345 | linked list cycle ii | 142 | 0.465 | Medium | 2,018 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2818708/Easy-two-pointers-Python-solution | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
fast = head
slow = head
while fast:
fast = fast.next
if fast:
fast = fast.next
slow = slow.next
if fast == slow:
... | linked-list-cycle-ii | Easy two pointers Python solution | spandei | 0 | 2 | linked list cycle ii | 142 | 0.465 | Medium | 2,019 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2723112/Python3-self-note-with-resources | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast: // find a loop
slow = head // slow start fr... | linked-list-cycle-ii | Python3 self-note with resources | vinafu0305 | 0 | 6 | linked list cycle ii | 142 | 0.465 | Medium | 2,020 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2711441/Python3-solution-using-object-address | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
node_cnt, seen_nodes_address = 0, {}
node = head
if not node:
return None
while True:
if node.next is None:
print("no cycle")
return None
... | linked-list-cycle-ii | Python3, solution using object address 💻 | evvfebruary | 0 | 4 | linked list cycle ii | 142 | 0.465 | Medium | 2,021 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2677157/Python3-set()-with-hash()-7-lines | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
hash_set = set()
while head:
current_hash = hash(head)
if current_hash in hash_set: return head
else: hash_set.add(current_hash)
head = head.next
return None | linked-list-cycle-ii | Python3 set() with hash() 7 lines | honeybadgerofdoom | 0 | 2 | linked list cycle ii | 142 | 0.465 | Medium | 2,022 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2599365/python3-oror-two-pointers | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow, fast = head, head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if slow == fast:
break
if not fast or not fast.next... | linked-list-cycle-ii | python3 || two pointers | rongjing | 0 | 50 | linked list cycle ii | 142 | 0.465 | Medium | 2,023 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2478365/Python-oror-Use-of-Floyd's-cycle-detection-cycle | class Solution:
def intersectionPoint(self, head: Optional[ListNode])-> Optional[ListNode]:
slow = fast = head
while fast != None and fast.next != None:
slow, fast = slow.next, fast.next.next
if slow == fast:
return slow
return None
def de... | linked-list-cycle-ii | Python || Use of Floyd's cycle detection cycle | sojwal_ | 0 | 33 | linked list cycle ii | 142 | 0.465 | Medium | 2,024 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2454096/easy-approach-in-python | class Solution:
def hasCycle(self , head):
slow , fast = head , head
while(fast and fast.next):
slow = slow.next
fast = fast.next.next
if(slow == fast):
break
return fast
def detectCycle(self, head: Optional[ListNode]) -> Optional[... | linked-list-cycle-ii | easy approach in python | rajitkumarchauhan99 | 0 | 64 | linked list cycle ii | 142 | 0.465 | Medium | 2,025 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2453869/Linked-List-Cycle-II | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
hs={}
while head:
if hs.get(head):
return head
hs[head]=1
head=head.next
return None
```
Using two pointer approach:
```
class Solution:
d... | linked-list-cycle-ii | Linked List Cycle II | sagar_gowda_t_p | 0 | 37 | linked list cycle ii | 142 | 0.465 | Medium | 2,026 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2397604/Python-Easy-Solution-or-Time%3A-O(N)-or-Beats-91 | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
visited = set()
cur = head
while cur:
if cur in visited:
return cur
visited.add(cur)
cur = cur.next | linked-list-cycle-ii | Python Easy Solution | Time: O(N) | Beats 91% | dos_77 | 0 | 76 | linked list cycle ii | 142 | 0.465 | Medium | 2,027 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2390205/Python-simple-solution | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
nodes = {}
while head:
if hash(head) in nodes:
return head
else:
nodes[hash(head)] = head
head = head.next
return None | linked-list-cycle-ii | Python simple solution | GMFB | 0 | 33 | linked list cycle ii | 142 | 0.465 | Medium | 2,028 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2355931/Easy-peasy-Solution-using-Stacks-55ms | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head: return head
elif not head.next: return None
cursor = head
stack = set()
while cursor:
stack.add(cursor)
if cursor.next in stack:
return cursor.next
cursor = cursor.next
return None | linked-list-cycle-ii | Easy-peasy Solution, using Stacks, 55ms | HappyLunchJazz | 0 | 66 | linked list cycle ii | 142 | 0.465 | Medium | 2,029 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2341579/Hash-set-vs-hash-table | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
d = set()
while head:
if head not in d:
d.add(head)
head = head.next
else:
return head
return None | linked-list-cycle-ii | Hash set vs hash table | granolan7 | 0 | 37 | linked list cycle ii | 142 | 0.465 | Medium | 2,030 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2341579/Hash-set-vs-hash-table | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
d = {}
while head:
if head not in d:
d[head] = 0
head = head.next
else:
return head
return None | linked-list-cycle-ii | Hash set vs hash table | granolan7 | 0 | 37 | linked list cycle ii | 142 | 0.465 | Medium | 2,031 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2244535/Python3-Solution-with-using-two-pointers | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head or not head.next :
return None
slow, fast = head.next, head.next.next
while fast and fast.next and slow != fast:
slow = slow.next
fast = fa... | linked-list-cycle-ii | [Python3] Solution with using two-pointers | maosipov11 | 0 | 43 | linked list cycle ii | 142 | 0.465 | Medium | 2,032 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2234679/Python-using-set-to-check-visited-nodes | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
seen = set()
node = head
while node:
seen.add(node)
if node.next in seen:
return node.next
node = node.next
return None | linked-list-cycle-ii | Python, using set to check visited nodes | blue_sky5 | 0 | 23 | linked list cycle ii | 142 | 0.465 | Medium | 2,033 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2180726/Python-Solution-or-O(1)-Space-or-With-Explanation | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
while head != None:
if head.val == 100001: ## If any node has value of 100001, we return the node
return head
head.val = 100001 ## Marking the node
head = head... | linked-list-cycle-ii | Python Solution | O(1) Space | With Explanation | prithuls | 0 | 105 | linked list cycle ii | 142 | 0.465 | Medium | 2,034 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2180726/Python-Solution-or-O(1)-Space-or-With-Explanation | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
## For any new node, add that to the set.
## If any node is already in the set, it means there is a cycle and that's the had of the cycle.
hm = set()
while head:
if head in hm:
ret... | linked-list-cycle-ii | Python Solution | O(1) Space | With Explanation | prithuls | 0 | 105 | linked list cycle ii | 142 | 0.465 | Medium | 2,035 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2100288/Simple-Python-solution-with-two-loops | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = head
fast = head
# first while loop to detect any cycle, slow pointer walks k steps and fast pointer walks 2k steps
while (fast and fast.next):
slow = slow.next
... | linked-list-cycle-ii | Simple Python solution with two loops | leqinancy | 0 | 34 | linked list cycle ii | 142 | 0.465 | Medium | 2,036 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2076420/Python-Solution-with-explanation | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = fast = head
#identifying if there is a cycle and identifying node where slow == fast
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
... | linked-list-cycle-ii | Python Solution with explanation | maj3r | 0 | 55 | linked list cycle ii | 142 | 0.465 | Medium | 2,037 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/2028211/Python-detailed-explanation-runtime-77.11-memory-28.96 | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
fast = head
while slow != fast:
... | linked-list-cycle-ii | Python detailed explanation, runtime 77.11%, memory 28.96% | tsai00150 | 0 | 71 | linked list cycle ii | 142 | 0.465 | Medium | 2,038 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/1988230/Python3-Easy-solution | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if (fast == slow): break
else:
return None
... | linked-list-cycle-ii | Python3 Easy solution | shrutipaul | 0 | 32 | linked list cycle ii | 142 | 0.465 | Medium | 2,039 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/1978950/Python-oror-Simple-Pointers | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
p1 = head
slow, fast = head, head
while fast and fast.next:
slow, fast = slow.next, fast.next.next
if slow == fast: break
if not fast or not fast.next: return None
while p1 != fast:
p1, fast = p1.next, fast.next
... | linked-list-cycle-ii | Python || Simple Pointers | morpheusdurden | 0 | 83 | linked list cycle ii | 142 | 0.465 | Medium | 2,040 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/1830381/Python3-solution-faster-than-98.46 | class Solution:
#Function to find starting node of loop in Linked list
def start(self,head,slow):
temp = head
while(temp != slow):
temp = temp.next
slow = slow.next
return temp
# use of Floyd's detection algorithm
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
... | linked-list-cycle-ii | Python3 solution - faster than 98.46% | KratikaRathore | 0 | 43 | linked list cycle ii | 142 | 0.465 | Medium | 2,041 |
https://leetcode.com/problems/linked-list-cycle-ii/discuss/1755984/Python3-oror-Simple-using-set-faster-than-90 | class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head or not head.next:
return None
s = set()
while head:
if head in s:
return head
s.add(head... | linked-list-cycle-ii | Python3 || Simple using set faster than 90% | Dewang_Patil | 0 | 21 | linked list cycle ii | 142 | 0.465 | Medium | 2,042 |
https://leetcode.com/problems/reorder-list/discuss/1640529/Python3-ONE-PASS-L(o)-Explained | class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
if not head.next or not head.next.next:
return
# search for the middle
slow, fast = head, head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.n... | reorder-list | ✔️ [Python3] ONE PASS, L(・o・)」, Explained | artod | 5 | 482 | reorder list | 143 | 0.513 | Medium | 2,043 |
https://leetcode.com/problems/reorder-list/discuss/1059760/Python.-faster-than-95.06.-O(n)-Super-simple-and-clear-solution. | class Solution:
def reorderList(self, head: ListNode) -> None:
if not head: return head
slow, fast = head, head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
prev, cur = None, slow.next
while cur:
save = cur.next
cur.next = prev
prev = cur
cur = save
slow.n... | reorder-list | Python. faster than 95.06%. O(n), Super simple & clear solution. | m-d-f | 5 | 707 | reorder list | 143 | 0.513 | Medium | 2,044 |
https://leetcode.com/problems/reorder-list/discuss/1990927/JavaC%2B%2BPythonJavaScriptKotlinSwiftO(n)timeBEATS-99.97-MEMORYSPEED-0ms-APRIL-2022 | class Solution:
def reverse(self , head):
prev = None
after = None
curr = head
while(curr):
after = curr.next
curr.next = prev
prev = curr
curr = after
return prev
def find_middle(self , head):
slow = he... | reorder-list | [Java/C++/Python/JavaScript/Kotlin/Swift]O(n)time/BEATS 99.97% MEMORY/SPEED 0ms APRIL 2022 | cucerdariancatalin | 3 | 170 | reorder list | 143 | 0.513 | Medium | 2,045 |
https://leetcode.com/problems/reorder-list/discuss/469198/Python-3-(seven-lines)-(88-ms) | class Solution:
def reorderList(self, H: ListNode) -> None:
if H == None: return None
C, A = H, []
while C != None: A.append(C); C = C.next
M = len(A)//2
for i in range(M): A[i].next, A[-(i+1)].next = A[-(i+1)], A[i+1]
A[M].next = None
return H
- Junaid ... | reorder-list | Python 3 (seven lines) (88 ms) | junaidmansuri | 3 | 810 | reorder list | 143 | 0.513 | Medium | 2,046 |
https://leetcode.com/problems/reorder-list/discuss/2448925/Python-Beats-99.75-with-full-working-explanation | class Solution:
def reorderList(self, head: Optional[ListNode]) -> None: # Time: O(n) and Space: O(1)
# Find Middle: find middle and divide the list in to two
slow, fast = head, head.next # head(slow) -> 1 -> 2(fast) -> ...
while fast and fast.next: # while fast exists and there is ne... | reorder-list | Python Beats 99.75% with full working explanation | DanishKhanbx | 2 | 134 | reorder list | 143 | 0.513 | Medium | 2,047 |
https://leetcode.com/problems/reorder-list/discuss/1668606/Concise-Python-3-implementation%3A-split-reverse-and-merge | class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
# find mid
slow = head
fast = head
while fast and fast.next:
slow = slow.next
fast = fast.n... | reorder-list | Concise Python 3 implementation: split, reverse and merge | MichaelRays | 1 | 40 | reorder list | 143 | 0.513 | Medium | 2,048 |
https://leetcode.com/problems/reorder-list/discuss/1640436/Python3-O(1)-Space-or-Fast-and-Slow-Pointers-or-Reverse-a-LinkedList-or-Iterative | class Solution:
def _findMid(self, head):
slow = fast = head
prev = None
while fast and fast.next:
prev = slow
slow = slow.next
fast = fast.next.next
return prev, slow
def _revList(self, endOfL2):
prev, cur = None, endOfL2
... | reorder-list | [Python3] O(1) Space | Fast and Slow Pointers | Reverse a LinkedList | Iterative | PatrickOweijane | 1 | 223 | reorder list | 143 | 0.513 | Medium | 2,049 |
https://leetcode.com/problems/reorder-list/discuss/1005838/Short-and-Recursive-Solution-in-Python-3 | class Solution:
def reorderList(self, head: ListNode) -> None:
def reorder(node: ListNode, position: int, length: int) -> None:
if (position < length / 2):
next = reorder(node.next, position + 1, length)
output = next.next
next.next = node.next
... | reorder-list | Short and Recursive Solution in Python 3 | mohandesi | 1 | 245 | reorder list | 143 | 0.513 | Medium | 2,050 |
https://leetcode.com/problems/reorder-list/discuss/707220/Python3-7-line-O(N)-time-(1)-space | class Solution:
def reorderList(self, head: ListNode) -> None:
"""
Do not return anything, modify head in-place instead.
"""
fast = slow = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
prev = None
while... | reorder-list | [Python3] 7-line O(N) time (1) space | ye15 | 1 | 129 | reorder list | 143 | 0.513 | Medium | 2,051 |
https://leetcode.com/problems/reorder-list/discuss/2786439/Python-more-generlized-solution-and-explanation-for-the-popular-solution | class Solution:
def reorderList(self, head):
#step 1: find middle
if not head: return []
slow, fast = head, head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
#step 2: reverse second half
prev, curr = None,... | reorder-list | Python more generlized solution and explanation for the popular solution | michaelniki | 0 | 3 | reorder list | 143 | 0.513 | Medium | 2,052 |
https://leetcode.com/problems/reorder-list/discuss/2737175/Simple-Approach-with-Queue-or-O(N)-or-O(N) | class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
q = deque()
current = head
while current:
q.append(current)
current = current.next
start = q.popleft(... | reorder-list | Simple Approach with Queue | O(N) | O(N) | sonnylaskar | 0 | 5 | reorder list | 143 | 0.513 | Medium | 2,053 |
https://leetcode.com/problems/reorder-list/discuss/2732662/Two-pointer-chaos | class Solution:
def reorderList(self, head: ListNode) -> None:
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
second = slow.next
slow.next = None
prev = None
while second:
tmp = second.ne... | reorder-list | Two pointer chaos | meechos | 0 | 5 | reorder list | 143 | 0.513 | Medium | 2,054 |
https://leetcode.com/problems/reorder-list/discuss/2726449/python-simple-sol. | class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
arr=[]
curr=head
while curr:
arr.append(curr.val)
curr=curr.next
curr=head
i=0
while curr!=None:
if i%2==0:
curr.val=arr.pop(0)
els... | reorder-list | python simple sol. | pranjalmishra334 | 0 | 13 | reorder list | 143 | 0.513 | Medium | 2,055 |
https://leetcode.com/problems/reorder-list/discuss/2506324/Python-Simple-solution-with-explanation-beats-97-runtime-65-memory | class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
#finding the midpoint, slow will be the end of the first half of the list
#if odd, it will be the midpoint, if even it will be the first of t... | reorder-list | Python Simple solution with explanation, beats 97% runtime, 65% memory | neeraj02 | 0 | 30 | reorder list | 143 | 0.513 | Medium | 2,056 |
https://leetcode.com/problems/reorder-list/discuss/2476625/10-lines-Easy-python-solution-with-visual-but-O(n2)-time | class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
if head.next is None or head.next.next is None:
return head
left1 = head
left2 = head.next
right1 = head
... | reorder-list | 10 lines Easy python solution with visual, but O(n^2) time | DerickDu | 0 | 49 | reorder list | 143 | 0.513 | Medium | 2,057 |
https://leetcode.com/problems/reorder-list/discuss/2331008/JavaC%2B%2BPythonJavaScriptKotlinSwiftO(n)timeBEATS-99.97-MEMORYSPEED-0ms-APRIL-2022 | class Solution:
def reverse(self , head):
prev = None
after = None
curr = head
while(curr):
after = curr.next
curr.next = prev
prev = curr
curr = after
return prev
def find_middle(self , head):
slow = he... | reorder-list | [Java/C++/Python/JavaScript/Kotlin/Swift]O(n)time/BEATS 99.97% MEMORY/SPEED 0ms APRIL 2022 | cucerdariancatalin | 0 | 61 | reorder list | 143 | 0.513 | Medium | 2,058 |
https://leetcode.com/problems/reorder-list/discuss/2330546/Python3-or-Fast-and-Slow-pointer-or-Easy-Understand-with-Comments | class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
dummy = ListNode(next = head)
slow = fast = head
n = 0
while fast and fast.next: # use fast, slow pointers to half the link... | reorder-list | Python3 | Fast and Slow pointer | Easy Understand with Comments | itachieve | 0 | 20 | reorder list | 143 | 0.513 | Medium | 2,059 |
https://leetcode.com/problems/reorder-list/discuss/2282433/Python-Solution-in-quadratic-time-with-explanation-(times-out-but-it's-correct) | class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
while head and head.next and head.next.next:
pre_tail = self._get_pre_tail(head)
tail = pre_tail.next
tail.next =... | reorder-list | [Python] Solution in quadratic time with explanation (times out, but it's correct) | julenn | 0 | 73 | reorder list | 143 | 0.513 | Medium | 2,060 |
https://leetcode.com/problems/reorder-list/discuss/2181783/Python-O(N)-Solution | class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
if not head:
return
lyst, cur = [], head
while cur:
lyst.append(cur)
cur = cur.next
l,r = 0,len(lyst)-1
# when l and r ptrs cross, we are done, lyst[l] is at tail
... | reorder-list | Python O(N) Solution | bjrshussain | 0 | 45 | reorder list | 143 | 0.513 | Medium | 2,061 |
https://leetcode.com/problems/reorder-list/discuss/1946506/Simple-python-solution | class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
slow ,fast = head, head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
#reversing the second half of the list
second = s... | reorder-list | Simple python solution | nikhitamore | 0 | 131 | reorder list | 143 | 0.513 | Medium | 2,062 |
https://leetcode.com/problems/reorder-list/discuss/1761305/Python-Soln | class Solution:
def reorderList(self, head: ListNode) -> None:
"""
Do not return anything, modify head in-place instead.
"""
arr=[]
curr=head
while curr:
arr.append(curr)
curr=curr.next
i,j=0,len(arr)-1
while i<j:#Point... | reorder-list | Python Soln | heckt27 | 0 | 29 | reorder list | 143 | 0.513 | Medium | 2,063 |
https://leetcode.com/problems/reorder-list/discuss/1641881/Python-3-simple-steps-explained.-Beats-87.55 | class Solution:
def reverse(self, head) -> ListNode:
prev, curr = None, head
# three pointers: last, current, next
while head:
head = head.next
curr.next = prev
prev = curr
curr = head
return prev
def merg... | reorder-list | [Python] 3 simple steps, explained. Beats 87.55% | mateoruiz5171 | 0 | 178 | reorder list | 143 | 0.513 | Medium | 2,064 |
https://leetcode.com/problems/reorder-list/discuss/1641522/Easy-Python-3-solution-via-list | class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
node = head
cur = head
li=[cur]
while cur.next != None:
cur = cur.next
li.append(cur)
last = None
for i in range(len(li)//2):
li[i].next = li[-1*(i+1)]
... | reorder-list | Easy Python 3 solution via list | annasweet2339 | 0 | 69 | reorder list | 143 | 0.513 | Medium | 2,065 |
https://leetcode.com/problems/reorder-list/discuss/1640821/python3-or-Time-O(n)-or-Space-O(1)-or-Easy-to-understand | class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
# Step 1 : Find the middle element of the linked list
slow=head
fast=head
while fast.next and fast.next.next:
... | reorder-list | python3 | Time - O(n) | Space -O(1) | Easy to understand | Rohit_Patil | 0 | 45 | reorder list | 143 | 0.513 | Medium | 2,066 |
https://leetcode.com/problems/reorder-list/discuss/1532255/Python3-solution | class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
nodes_idx = defaultdict(int)
temp = head
count = 1
while temp:
nodes_idx[count] = temp
count += 1
... | reorder-list | Python3 solution | akshaykumar19002 | 0 | 143 | reorder list | 143 | 0.513 | Medium | 2,067 |
https://leetcode.com/problems/reorder-list/discuss/1500996/Python3-Two-kind-of-solution-O(n)-space-and-O(1)-space | class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
if not head:
return None
stack = []
cur = head
while cur:
stack.append(cur)
cur = cur.next
cur = head
while cur and cur != stack... | reorder-list | [Python3] Two kind of solution O(n) space and O(1) space | maosipov11 | 0 | 84 | reorder list | 143 | 0.513 | Medium | 2,068 |
https://leetcode.com/problems/reorder-list/discuss/1432744/Zip-two-times-the-list-of-references-99.9-speed | class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
curr = head
lst_ref = []
while curr:
lst_ref.append(curr)
curr = curr.next
if len(lst_ref) <= 2:
return
half_len, rem = divmod(len(lst_ref), 2)
if rem:
... | reorder-list | Zip two times the list of references, 99.9% speed | EvgenySH | 0 | 146 | reorder list | 143 | 0.513 | Medium | 2,069 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/1403244/3-Simple-Python-solutions | class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
ans = []
stack = [root]
while stack:
temp = stack.pop()
if temp:
ans.append(temp.val)
stack.append(temp.right) #as we are using st... | binary-tree-preorder-traversal | 3 Simple Python solutions | shraddhapp | 14 | 1,100 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,070 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/1403244/3-Simple-Python-solutions | class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
ans = []
if not root:
return ans
def preorder(node):
if node:
ans.append(node.val)
preorder(node.left)
preorder(node.right)
... | binary-tree-preorder-traversal | 3 Simple Python solutions | shraddhapp | 14 | 1,100 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,071 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/1403244/3-Simple-Python-solutions | class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right) | binary-tree-preorder-traversal | 3 Simple Python solutions | shraddhapp | 14 | 1,100 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,072 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/2443518/Easy-oror-0-ms-oror-100-oror-Fully-Explained-(Java-C%2B%2B-Python-Python3)-oror-Recursive-oror-Iterative | class Solution(object):
def preorderTraversal(self, root):
# Create an empty stack and push the root node...
bag = [root]
# Create an array list to store the solution result...
sol = []
# Loop till stack is empty...
while bag:
# Pop a node from the stack..... | binary-tree-preorder-traversal | Easy || 0 ms || 100% || Fully Explained (Java, C++, Python, Python3) || Recursive || Iterative | PratikSen07 | 9 | 586 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,073 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/1765377/Python3-or-easiest-solution | class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
res = []
if root:
res+= [root.val]
res+= self.preorderTraversal(root.left)
res+= self.preorderTraversal(root.right)
return res | binary-tree-preorder-traversal | Python3 | easiest solution | Anilchouhan181 | 6 | 239 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,074 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/1765397/Python3-or-fastest-solution-or-Recursion-or-One-Liner | class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if root is None:
return []
return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right) | binary-tree-preorder-traversal | Python3 | fastest solution | Recursion | One Liner | Anilchouhan181 | 4 | 166 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,075 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/653895/Python3-preorder-traversal-recursively-and-iteratively | class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
ans = []
if root:
stack = [root]
while stack:
node = stack.pop()
ans.append(node.val)
if node.right: stack.append(node.right)
... | binary-tree-preorder-traversal | [Python3] preorder traversal recursively & iteratively | ye15 | 3 | 278 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,076 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/653895/Python3-preorder-traversal-recursively-and-iteratively | class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
ans, stack = [], [root]
while stack:
node = stack.pop()
if node:
ans.append(node.val)
stack.append(node.right)
stack.append(node.left)
... | binary-tree-preorder-traversal | [Python3] preorder traversal recursively & iteratively | ye15 | 3 | 278 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,077 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/2352690/Python-simple-8-Line-iterative-and-recursive-with-explanation | class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
# Store nodes
ans = []
# Recursive function
def preorder_traversal(root):
# Base case
if not root: return
else:
# Record node
ans.append(root.val)
# Travel left t... | binary-tree-preorder-traversal | Python simple 8 Line iterative and recursive with explanation | drblessing | 2 | 91 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,078 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/2352690/Python-simple-8-Line-iterative-and-recursive-with-explanation | class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
# Init store
ans = []
# Init stack
stack = [root]
# Iterate through stack
while stack:
node = stack.pop()
# Base case
if not node:
... | binary-tree-preorder-traversal | Python simple 8 Line iterative and recursive with explanation | drblessing | 2 | 91 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,079 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/2315908/144.-My-Python-Solution | class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
return [root.val]+self.preorderTraversal(root.left)+self.preorderTraversal(root.right) | binary-tree-preorder-traversal | 144. My Python Solution | JunyiLin | 1 | 26 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,080 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/2315908/144.-My-Python-Solution | class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
stack = []
res = []
while root or stack:
while root:
res.append(root.val)
stack.append(root)
root=root.left
root = stack.pop()
... | binary-tree-preorder-traversal | 144. My Python Solution | JunyiLin | 1 | 26 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,081 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/1508527/Python3-simple-recursive-solution | class Solution:
def __init__(self):
self.ans = []
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return
self.ans.append(root.val)
if root.left:
self.preorderTraversal(root.left)
if root.right:
self.preo... | binary-tree-preorder-traversal | Python3 simple recursive solution | EklavyaJoshi | 1 | 47 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,082 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/1342451/Python-Clean-Iterative-DFS | class Solution:
def preorderTraversal(self, root: TreeNode) -> List[int]:
if not root:
return []
preorder, stack = [], [root]
while stack:
node = stack.pop()
if not node:
continue
preorder.append(node.val)
s... | binary-tree-preorder-traversal | [Python] Clean Iterative DFS | soma28 | 1 | 162 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,083 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/988724/C%2B%2BPython-Simple-Solution | class Solution:
def preorderTraversal(self, root: TreeNode) -> List[int]:
st=[];l=[]
while root or st:
while root:
st.append(root)
l.append(root.val)
root=root.left
root=st.pop().right
return l | binary-tree-preorder-traversal | [C++/Python] Simple Solution | lokeshsenthilkumar | 1 | 191 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,084 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/2835819/recursive-approach-or-fastest-solution-or-Python3 | class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if not root: return []
return [root.val,*self.preorderTraversal(root.left),*self.preorderTraversal(root.right)] | binary-tree-preorder-traversal | recursive approach | fastest solution | Python3 | pardunmeplz | 0 | 1 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,085 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/2828861/python-oror-simple-solution-oror-recursion | class Solution:
def preorderTraversal(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-preorder-traversal | python || simple solution || recursion | wduf | 0 | 2 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,086 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/2797855/easy-solution | class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
elementsList = []
self.check(root,elementsList)
return elementsList
def check(self,root,lst):
if root:
lst.append(root.val)
self.check(root.left,lst)
... | binary-tree-preorder-traversal | easy solution | betaal | 0 | 2 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,087 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/2797307/Python3-iterative-constant-memory-solution | class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
out = []
while True:
if not root: return out # last node reached
out.append(root.val) # visit node
if not root.left: root = root.right # no left child ... | binary-tree-preorder-traversal | Python3 iterative constant memory solution | lucabonagd | 0 | 3 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,088 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/2788993/python | class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
ans = []
stk = [root]
while stk:
node = stk.pop()
ans.append(node.val)
if node.right:
stk.append(node.right)
... | binary-tree-preorder-traversal | python | xy01 | 0 | 1 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,089 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/2785936/Python-or-Recursive-solution | class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
current = root
if not current:
return []
return [current.val] + self.preorderTraversal(current.left) + self.preorderTraversal(current.right) | binary-tree-preorder-traversal | Python | Recursive solution | Clarai | 0 | 1 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,090 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/2766898/Iterative-Easiest-in-python | class Solution:
# Fastest and Easiest Preorder iterative solution
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
res = []
if not root: return res
queue = deque([root])
while queue:
ele = queue.popleft()
if type(ele) == int:
... | binary-tree-preorder-traversal | Iterative Easiest in python | shiv-codes | 0 | 6 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,091 |
https://leetcode.com/problems/binary-tree-preorder-traversal/discuss/2763901/python3-sol. | class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
ans = []
stack = [root]
while stack:
node = stack.pop()
if node:
ans.append(node.val)
stack.append(node.right)
stack.append(node.left)
return ans | binary-tree-preorder-traversal | python3 sol. | pranjalmishra334 | 0 | 2 | binary tree preorder traversal | 144 | 0.648 | Easy | 2,092 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/2443702/Easy-oror-Recursive-and-Iterative-oror-100-oror-Explained-(Java-C%2B%2B-Python-Python3) | class Solution(object):
def postorderTraversal(self, root):
# Base case...
if not root: return []
# Create an array list to store the solution result...
sol = []
# Create an empty stack and push the root node...
bag = [root]
# Loop till stack is empty...
... | binary-tree-postorder-traversal | Easy || Recursive & Iterative || 100% || Explained (Java, C++, Python, Python3) | PratikSen07 | 16 | 1,100 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,093 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/905483/Python-iterative-solution | class Solution:
def postorderTraversal(self, root: TreeNode) -> List[int]:
result=[]
stack=[]
while root or stack:
while root:
stack.append(root) # push nodes into the stack
root=root.left if root.left else root.right
root=stack.pop()
... | binary-tree-postorder-traversal | Python iterative solution | holystraw | 9 | 1,200 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,094 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/1342471/Python-Clean-Iterative-DFS | class Solution:
def postorderTraversal(self, root: TreeNode) -> List[int]:
if not root:
return []
postorder, stack = [], [root]
while stack:
node = stack.pop()
if not node:
continue
postorder.append(node.val)
stack... | binary-tree-postorder-traversal | [Python] Clean Iterative DFS | soma28 | 6 | 657 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,095 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/1342471/Python-Clean-Iterative-DFS | class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
post_order, stack = [], []
node = root
while stack or node:
while node:
if node.right:
stack.append(node.right)
... | binary-tree-postorder-traversal | [Python] Clean Iterative DFS | soma28 | 6 | 657 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,096 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/2466951/Python-simple-solution-beats-91 | class Solution:
def __init__(self):
self.postOrder = []
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if root is None:
return []
if root:
self.postorderTraversal(root.left)
self.postorderTraversal(root.right)
self.pos... | binary-tree-postorder-traversal | Python simple solution beats 91% | aruj900 | 3 | 114 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,097 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/1765414/99.52-Faster-or-Python3-Solution-or-Recursively | class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if root is None:
return []
return self.postorderTraversal(root.left)+self.postorderTraversal(root.right)+[root.val] | binary-tree-postorder-traversal | ✔ 99.52% Faster | Python3 Solution | Recursively | Anilchouhan181 | 3 | 139 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,098 |
https://leetcode.com/problems/binary-tree-postorder-traversal/discuss/1401746/Easy-fast-Python-recursive-solution-or-3-lines-of-code | class Solution:
def postorderTraversal(self, root: TreeNode) -> List[int]:
if not(root):
return []
return self.postorderTraversal(root.left) + self.postorderTraversal(root.right) + [root.val] | binary-tree-postorder-traversal | Easy fast Python recursive solution | 3 lines of code | Kamil732 | 3 | 227 | binary tree postorder traversal | 145 | 0.668 | Easy | 2,099 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.