File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
leetcode.com 86. Partition List Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ from typing import Optional
2
+
3
+
4
+ # Definition for singly-linked list.
5
+ class ListNode :
6
+ def __init__ (self , val = 0 , next = None ):
7
+ self .val = val
8
+ self .next = next
9
+
10
+
11
+ class Solution :
12
+ def partition (self , head : Optional [ListNode ], x : int ) -> Optional [ListNode ]:
13
+ lessthan_x = ListNode (- 201 , None )
14
+ greaterthanorequal_x = ListNode (201 , None )
15
+ lx , gx = lessthan_x , greaterthanorequal_x
16
+
17
+ node = head
18
+
19
+ while node :
20
+ tmp = node
21
+ node = node .next
22
+
23
+ if tmp .val < x :
24
+ lx .next = tmp
25
+ lx = tmp
26
+ lx .next = None
27
+ else :
28
+ gx .next = tmp
29
+ gx = tmp
30
+ gx .next = None
31
+
32
+ lx .next = greaterthanorequal_x .next
33
+
34
+ return lessthan_x .next
Original file line number Diff line number Diff line change
1
+ from unittest import TestCase
2
+ from main import Solution
3
+
4
+ class TestSolution (TestCase ):
5
+ def test1_partition (self ):
6
+ sln = Solution ()
7
+ head = ListNode
You can’t perform that action at this time.
0 commit comments