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 try: slow_p = slow_p.next fast_p = fast_p.next.next except: return False 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,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 False
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 head.val = 10000000 head = head.next return False
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_one: return True return False
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 linkedlist while fast: # advance fast fast = fast.next # every other loop if cnt % 2: # advance slow slow = slow.next # if fast and slow point to the same node, we have a loop if fast is slow: return True # increment cnt cnt += 1 return False
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): fast = fast.next slow = slow.next return slow return None
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: slow = slow.next fast = fast.next return fast 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,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 = lo.next hi = hi.next return 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.add(curr) print(curr) if curr.next in visited: return curr.next curr = curr.next print(visited)
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==fast: break if fast==None or fast.next==None: return None slow = head pos = 0 while fast!=slow: slow=slow.next fast=fast.next return 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 return None
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 if slow==fast: while slow!=entry: entry = entry.next slow = slow.next return entry return None
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: break if f == s: while f != head: f = f.next head = head.next return head else: return None
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: while head != slow: head = head.next slow = slow.next return head return None
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 from beginning while slow!=fast: // keep going until reach the entry of loop slow = slow.next fast = fast.next return slow // now slow == fast, reach the entry of the loop.
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 node_address = hex(id(node)) if node_address in seen_nodes_address.keys(): print(f"tail connects to node index {seen_nodes_address[node_address]}") return node else: seen_nodes_address[node_address] = node_cnt node = node.next node_cnt += 1
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: return None slow = head while slow != fast: fast = fast.next slow = slow.next return slow
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 detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: obj = Solution() intersection = obj.intersectionPoint(head) if intersection == None: return None else: slow = head while slow != intersection: slow, intersection = slow.next, intersection.next return slow
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[ListNode]: fast = self.hasCycle(head) if(fast == None or fast.next == None): return None slow = head while(fast != slow): slow = slow.next fast = fast.next return slow
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: def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: slow,fast=head,head cycled=False while fast and fast.next: slow=slow.next fast=fast.next.next if slow==fast: cycled=True break if cycled: while head!=fast: head=head.next fast=fast.next return head
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 = fast.next.next slow = head while fast and fast.next and slow != fast: slow = slow.next fast = fast.next return slow if fast and fast.next and fast.next.next else None
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.next return None ## No cycle
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: return head hm.add(head) head = head.next return None
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 fast = fast.next.next # two pointers meet k steps for slow and 2k for fast pointer # which is m steps from cycle start point # cycle start point is (k-m) steps from head pointer, thus we need second loop to track (k-m)'s value if (slow == fast): slow = head break # return None if no cycle exists if (not fast or not fast.next): return None; # second for loop for both slow and fast to walk (k-m) steps to meet again at the cycle start point while (slow != fast): slow = slow.next fast = fast.next return slow
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: break #if not a cycle then returning None if not fast or not fast.next: return None # starting from head again and iterating both slow and fast 1 step at a time. Returning the node that is same for both. that Node is the meeting point slow = head while slow: if slow == fast: return slow slow = slow.next fast = fast.next
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: slow = slow.next fast = fast.next return fast return None
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 slow = head while (fast != slow): slow = slow.next fast = fast.next return slow
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 return p1
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]: if (head == None): return None if(head.next == None): return None slow = head fast = head while(fast and fast.next and fast.next.next): fast = fast.next.next slow = slow.next if(slow == fast): # loop presents start = self.start(head,slow) return start # no loop return None
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) head = head.next return 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.next tail, cur = None, slow.next slow.next = None # detach list on the middle # reverse right part while cur: cur.next, tail, cur = tail, cur, cur.next # rearrange nodes as asked headCur, headNext = head, head.next tailCur, tailNext = tail, tail.next while True: headCur.next, tailCur.next = tailCur, headNext if not tailNext: return tailCur, headCur = tailNext, headNext tailNext, headNext = tailNext.next, headNext.next
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.next = None head2 = prev while head2: save1 = head.next head.next = head2 head = head2 head2 = save1
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 = head fast = head while(fast and fast.next): fast = fast.next.next slow = slow.next return slow def reorderList(self, head: Optional[ListNode]) -> None: mid = self.find_middle(head) rev = self.reverse(mid) first = head second = rev while(second.next): temp = first.next first.next = second first = temp temp = second.next second.next = first second = temp
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 Mansuri - Chicago, IL
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 next element to travel keep moving slow = slow.next # fast moving twice as much as slow, will lead slow to point in the middle fast = fast.next.next # Even(4): slow = 2, fast = 4 & Odd(5): slow = 3, fast = None # Reverse: reverse the second list second = slow.next # in Odd case lets say 1-> 2(slow) -> 3 -> 4(fast): second = 3(2.next) prev = slow.next = None # Created Two separate nodes 1->2 & 3->4 while second: tmp = second.next # tmp = 4 second.next = prev # 3 -> None prev = second # prev = 3 second = tmp # second = 4 # So, in the next iteration # tmp = None # 4.next = prev(3) and our linked is reversed # prev = 4 # second = None # Merge: merge the first with the reversed second first, second = head, prev # first will point to starting of the 1st Node and second to 2nd Node while second: tmp1, tmp2 = first.next, second.next # tmp1 = 2, tmp2 = 3 first.next = second # 1 -> 4 second.next = tmp1 # 4.next = 2 i.e. 1 -> 4 -> 2 first, second = tmp1, tmp2 # first = 2, second = 3 # So, in the next iteration # tmp1 = tmp2 = None # 2 -> 3 i.e. 1 -> 4 -> 2 -> 3 # 1 -> 4 -> 2 -> 3 -> None # first = second = None
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.next.next second_start = slow.next slow.next = None second_start_reversed = self._reverse(second_start) self._merge(head, second_start_reversed) def _reverse(self, head): dummy = None curr = head while curr: n = curr.next curr.next = dummy dummy = curr curr = n return dummy def _merge(self, list1, list2): dummy = ListNode(0) curr = dummy while list1 or list2: if list1: curr.next = list1 list1 = list1.next curr = curr.next if list2: curr.next = list2 list2 = list2.next curr = curr.next return dummy.next
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 while cur: nextCur = cur.next cur.next = prev prev, cur = cur, nextCur return prev def reorderList(self, head: Optional[ListNode]) -> None: if not head: return None if not head.next: return head endOfL1, endOfL2 = self._findMid(head) endOfL1.next = None l1 = head l2 = self._revList(endOfL2) dummy = tail = ListNode() isL1 = True while l1 and l2: if isL1: tail.next = l1 l1 = l1.next else: tail.next = l2 l2 = l2.next isL1 = not isL1 tail = tail.next if l1: tail.next = l1 if l2: tail.next = l2 return dummy.next
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 node.next = next else: next = node.next if (length % 2 == 1): node.next = None output = next else: output = next.next next.next = None return output node = head length = 0 while (node): node = node.next length += 1 length and reorder(head, 1, length)
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 slow: prev, slow.next, slow = slow, prev, slow.next node = head while prev and prev.next: node.next, node = prev, node.next prev.next, prev = node, prev.next
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, slow.next while curr: nextt = curr.next curr.next = prev prev = curr curr = nextt slow.next = None #step 3: merge lists head1, head2 = head, prev while head2: nextt = head1.next head1.next = head2 head1 = head2 head2 = nextt
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() popleft = False while q: if popleft: start.next = q.popleft() else: start.next = q.pop() popleft = not popleft last = start start = start.next start.next = None #to remove the last node's next
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.next second.next = prev prev = second second = tmp first, second = head, prev while second: tmp1, tmp2 = first.next, second.next first.next = second second.next = tmp1 first = tmp1 second = tmp2
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) else: curr.val=arr.pop() curr=curr.next i+=1
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 the two mid elements slow, fast = head, head.next while fast and fast.next: fast = fast.next.next slow = slow.next #making slow.next as the start of SECond list sec = slow.next #breaking the connection between first and second list slow.next = None #reversing the second list prev, curr = None, sec while curr: temp = curr.next curr.next = prev prev = curr curr = temp #making start as the head of second list start = prev # traversing both the lists and swapping nodes while start and head: temp = head.next temp2 = start.next head.next = start head.next.next = temp start = temp2 head = head.next.next return
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 while right1.next.next: right1 = right1.next right2 = right1.next left1.next = right2 right1.next = None right2.next = self.reorderList(left2) return left1
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 = head fast = head while(fast and fast.next): fast = fast.next.next slow = slow.next return slow def reorderList(self, head: Optional[ListNode]) -> None: mid = self.find_middle(head) rev = self.reverse(mid) first = head second = rev while(second.next): temp = first.next first.next = second first = temp temp = second.next second.next = first second = temp
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 linked list fast = fast.next.next slow = slow.next n += 1 if not n: # check if the linked list only contains one node return head list2 = slow reversed_list2 = self.reverseList(list2) prev, curr = None, head while curr != slow: # move the curr pointer before the slow linked list, so the dummy.next is the first half prev = curr curr = curr.next prev.next = None list1 = dummy.next dummy.next = self.mergeList(list1, reversed_list2) return dummy.next def reverseList(self, head: Optional[ListNode]): prev, curr = None, head while curr: temp = curr.next curr.next = prev prev = curr curr = temp return prev def mergeList(self, list1: Optional[ListNode], list2:Optional[ListNode]): if not list1: return list2 if not list2: return list1 list1.next = self.mergeList(list2, list1.next) return list1
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 = head.next head.next = tail pre_tail.next = None head = head.next if head: head = head.next @staticmethod def _get_pre_tail(node): if not node or not node.next: return None while node.next.next: node = node.next return node
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 while l < r: lyst[l].next = lyst[r] l += 1 lyst[r].next = lyst[l] r -= 1 lyst[l].next = None return lyst[0]
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 = slow.next prev = slow.next = None while second: temp = second.next second.next = prev #null intially prev = second second = temp #merge 2 halves first, second = head, prev while second: tmp1, tmp2 = first.next, second.next #storing the link between node first.next = second second.next = tmp1 #shift pointers first ,second = tmp1,tmp2
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 the next of i node to j and j to i+1 node arr[i].next=arr[j] if i+1<j: arr[j].next=arr[i+1] i+=1 j-=1 arr[i].next=None
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 merge(self, h1, h2) -> None : curr = ListNode(0) while h1 and h2: curr.next = h1 h1 = h1.next curr = curr.next curr.next = h2 h2 = h2.next curr = curr.next curr.next = h1 or h2 def reorderList(self, head: Optional[ListNode]) -> None: """ Do not return anything, modify head in-place instead. """ if not head.next: return head # algo is made up of: find middle, split, reverse, merge # find middle: fast and slow pointers slow, fast = head, head.next while fast and fast.next: fast = fast.next.next slow = slow.next # split temp = slow.next slow.next = None # reverse temp = self.reverse(temp) # merge self.merge(head, temp)
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)] if i!=((len(li)//2)-1): li[-1*(i+1)].next = li[i+1] else: li[-1*(i+1)].next = None last = li[-1*(i+1)] if ((len(li)%2)==1) and (len(li)!=1): last.next = li[len(li)//2] li[len(li)//2].next = None return node
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: slow=slow.next fast=fast.next.next head2=slow.next slow.next=None # Step 2 : reverse the linked list after the mid point prev=None curr=head2 while curr: temp=curr.next curr.next=prev prev=curr curr=temp head2=prev # Step 3: add head1 and head2 in alternate manner head1=head while head1 and head2: temp1=head1.next temp2=head2.next head1.next=head2 head2.next=temp1 head2=temp2 head1=temp1
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 temp = temp.next nodes_idx[count -1].next = None count -= 1 if count == 1: return nodes_idx[1] start = 1 end = count head = None temp = None while start < end: if not head: head = nodes_idx[start] temp = head temp.next = nodes_idx[end] temp = temp.next else: temp.next = nodes_idx[start] temp = temp.next temp.next = nodes_idx[end] temp = temp.next start += 1 end -= 1 if start == end: temp.next = nodes_idx[start] return head
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[-1]: _next = cur.next cur.next = stack.pop() if cur.next != _next: stack[-1].next = None cur.next.next = _next cur = cur.next.next return head class Solution: def reorderList(self, head: Optional[ListNode]) -> None: if not head: return None slow = fast = head while fast and fast.next: slow = slow.next fast = fast.next.next prev = None _next = None while slow: _next = slow.next slow.next = prev prev = slow slow = _next cur = head while cur != prev and cur.next != prev: begin_next = cur.next end_next = prev.next cur.next = prev prev.next = begin_next prev = end_next cur = cur.next.next return head
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: for a, b in zip(lst_ref[:half_len:1], lst_ref[len(lst_ref) - 1: half_len:-1]): a.next = b for a, b in zip(lst_ref[len(lst_ref) - 1: half_len + 1:-1], lst_ref[1:half_len:1]): a.next = b lst_ref[half_len + 1].next = lst_ref[half_len] else: for a, b in zip(lst_ref[:half_len:1], lst_ref[len(lst_ref) - 1: half_len - 1:-1]): a.next = b for a, b in zip(lst_ref[len(lst_ref) - 1: half_len:-1], lst_ref[1:half_len:1]): a.next = b lst_ref[half_len].next = None
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 stack which works on LIFO, we need to push right tree first so that left will be popped out stack.append(temp.left) return ans
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) preorder(root) return ans
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... node = bag.pop() if node: sol.append(node.val) # Append the right child of the popped node into the stack bag.append(node.right) # Push the left child of the popped node into the stack bag.append(node.left) return sol # Return the solution list...
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) if node.left: stack.append(node.left) return ans
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) return ans
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 then right preorder_traversal(root.left) preorder_traversal(root.right) preorder_traversal(root) return ans
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: continue # Log value ans.append(node.val) # Add right and left nodes to the stack stack.append(node.right) stack.append(node.left) return ans
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() root = root.right return res
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.preorderTraversal(root.right) return self.ans
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) stack.append(node.right) stack.append(node.left) return preorder
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: return # first, check cur val ans.append(node.val) # second, check left child traverse(node.left) # third, check right child traverse(node.right) # fill ans traverse(root) return ans
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) self.check(root.right,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 -> try right child elif not root.right: root = root.left # no right child -> try left child elif root.left is root.right: # backpointer node -> continue on that subtree l = root.left root.left, root.right = None, None # remove backpointer root = l else: # both children -> set left subtree's last node as backpointer to right subtree l = root.left while True: # find left subtree's last node if l.right: l = l.right elif l.left: l = l.left else: break l.right = l.left = root.right # set backpointer to right subtree root = root.left # goto 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) if node.left: stk.append(node.left) return ans
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: res.append(ele) elif type(ele) == TreeNode and ele.left == None and ele.right == None: res.append(ele.val) else: if ele.right: queue.appendleft(ele.right) if ele.left: queue.appendleft(ele.left) queue.appendleft(ele.val) return res
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... while bag: # Pop a node from the stack... node = bag.pop() sol.append(node.val) # Push the left child of the popped node into the stack... if node.left: bag.append(node.left) # Append the right child of the popped node into the stack... if node.right: bag.append(node.right) return sol[::-1] # Return the solution list...
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() result.append(root.val) #Deal with the root node whenever it is popped from stack if stack and stack[len(stack)-1].left==root: #check whether it has been traversed root=stack[len(stack)-1].right else: root=None #Force to quit the loop return(result)
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.append(node.left) stack.append(node.right) return postorder[::-1]
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) stack.append(node) node = node.left last = stack.pop() if last.right and stack and last.right == stack[-1]: node = stack.pop() stack.append(last) else: post_order.append(last.val) return post_order
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.postOrder.append(root.val) return self.postOrder
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