Leetcode # 86. Partition List
- 2023.08.15
- ★★ Medium LeetCode Linked List
https://leetcode.com/problems/partition-list
Solution
ge_cur.next = cur
ge_cur = cur
→
ge_cur = ge_cur.next = cur
⇒ 1.
ge_cur
← cur
2.
ge_cur.next
≡ cur.next
← cur
⇒ loop
ge_cur.next = ge_cur = cur
⇒ 1.
ge_cur.next
← cur
2.
ge_cur
← cur
參考 多重賦值 (Multiple Assignment) 時的計算順序
Time Complexity: O(n), n := length of linked list
Space Complexity: O(1)
(The input and output generally do not count towards the space complexity.)
class Solution: def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]: ls_head, ge_head = ListNode(), ListNode() ls_cur, ge_cur, cur = ls_head, ge_head, head while cur: if cur.val >= x: ge_cur.next = ge_cur = cur cur = cur.next ge_cur.next = None else: ls_cur.next = ls_cur = cur cur = cur.next ls_cur.next = None ls_cur.next = ge_head.next return ls_head.next
Last Updated on 2023/08/16 by A1go