Created
January 8, 2020 17:11
-
-
Save Desolve/ba6d096a47b698a49714560a9c591f88 to your computer and use it in GitHub Desktop.
0086 Partition List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definition for singly-linked list. | |
# class ListNode: | |
# def __init__(self, x): | |
# self.val = x | |
# self.next = None | |
class Solution: | |
def partition(self, head: ListNode, x: int) -> ListNode: | |
d1, d2 = ListNode(0), ListNode(0) | |
ite = [d1, d2] | |
while head: | |
nxt = head.next | |
i = 0 if head.val < x else 1 | |
ite[i].next = head | |
ite[i] = ite[i].next | |
ite[i].next = None; | |
head = nxt | |
if not d1.next: return d2.next | |
if not d2.next: return d1.next | |
ite[0].next = d2.next | |
return d1.next |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment