Skip to content

Commit 4b48634

Browse files
committed
leetcode.com 86. Partition List
문제 링크: https://leetcode.com/problems/partition-list
1 parent 33f3a67 commit 4b48634

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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

0 commit comments

Comments
 (0)