Skip to content

Commit

Permalink
added python code partition list (#21)
Browse files Browse the repository at this point in the history
Co-authored-by: lantin2016 <[email protected]>
  • Loading branch information
lantin2016 and lantin2016 authored Dec 4, 2024
1 parent bd4e455 commit bd2eaa5
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/linked-list/partition-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ defaultValue="java"
values={[
{ label: 'Java', value: 'java', },
{ label: 'C++', value: 'cpp', },
{ label: 'Python', value: 'python', },
]
}>
<TabItem value="java">
Expand Down Expand Up @@ -91,5 +92,34 @@ public:
};
```

</TabItem>
<TabItem value="python">

```python
# Partition List
# 时间复杂度O(n),空间复杂度O(1)
class Solution:
def partition(self, head, x):
left_dummy = ListNode(-1) # 头结点
right_dummy = ListNode(-1) # 头结点

left_cur = left_dummy
right_cur = right_dummy
cur = head
while cur is not None:
if cur.val < x:
left_cur.next = cur
left_cur = cur
else:
right_cur.next = cur
right_cur = cur
cur = cur.next

left_cur.next = right_dummy.next
right_cur.next = None

return left_dummy.next
```

</TabItem>
</Tabs>

0 comments on commit bd2eaa5

Please sign in to comment.