https://leetcode.com/problems/partition-list/

class Solution {
    func partition(_ head: ListNode?, _ x: Int) -> ListNode? {
        var smaller = [ListNode]()
        var bigger = [ListNode]()
        var node = head
        while let n = node {
            if n.val < x {
                smaller.append(n)
            } else {
                bigger.append(n)
            }
            node = n.next
        }
        guard !(smaller.isEmpty || bigger.isEmpty) else { return head }
        for i in 0..<smaller.count-1 {
            smaller[i].next = smaller[i+1]
        }
        smaller.last?.next = bigger.first
        for i in 0..<bigger.count-1 {
            bigger[i].next = bigger[i+1]
        }
        bigger.last?.next = nil
        return smaller.first == nil ? bigger.first : smaller.first
    }
}

Runtime: 12 ms, faster than 100.00% of Swift online submissions for Partition List.
Memory Usage: 19 MB, less than 29.98% of Swift online submissions for Partition List.