-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathpartition-list.py
More file actions
35 lines (25 loc) · 806 Bytes
/
Copy pathpartition-list.py
File metadata and controls
35 lines (25 loc) · 806 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from __future__ import annotations
from dataclasses import dataclass
from typing import TYPE_CHECKING, Optional
if TYPE_CHECKING:
@dataclass
class ListNode:
val: int = 0
next: Optional[ListNode] = None
class Solution:
def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:
left_head = ListNode()
left_tail = left_head
right_head = ListNode()
right_tail = right_head
node = head
while node:
if node.val < x:
left_tail.next = node
left_tail = node
else:
right_tail.next = node
right_tail = node
node.next, node = None, node.next
left_tail.next = right_head.next
return left_head.next