# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def partition(self, head: ListNode, x: int) -> ListNode:
        g_head = greater = ListNode(0)
        l_head = less = ListNode(0)
        while head:
            if head.val < x:
                less.next = head
                less = less.next
            else:
                greater.next = head
                greater = greater.next
            head = head.next
        greater.next = None # don't forget to cut down to avoid circle
        less.next = g_head.next
        return l_head.next
        
