<commit_before>/**
 * Question Link: https://leetcode.com/problems/merge-k-sorted-lists/
 * Primary idea: Divide and Conqure with merge two sorted lists as a helper function
 * Time Complexity: O(mlogn), m stands for the length of one list, n stands for the number of lists
 *                  Space Complexity: O(1)
 *
 * Definition for singly-linked list.
 * public class ListNode {
 *     public var val: Int
 *     public var next: ListNode?
 *     public init(_ val: Int) {
 *         self.val = val
 *         self.next = nil
 *     }
 * }
 */

class MergeKSortedLists {
    func mergeKLists(lists: [ListNode?]) -> ListNode? {
        guard lists.count > 0 else {
            return nil
        }
    
        var left = 0
        var right = lists.count - 1
        
        var lists = lists
        
        while right > 0 {
            left = 0
            while left < right {
                lists[left] = _mergeTwoLists(lists[left], lists[right])
                left += 1
                right -= 1
            }
        }
        
        return lists[0]
    }
    
    private func _mergeTwoLists(l1: ListNode?, _ l2: ListNode?) -> ListNode? {
        let dummy = ListNode(0)
        var node = dummy
    
        var l1 = l1
        var l2 = l2
        
        while l1 != nil && l2 != nil {
            if l1!.val < l2!.val {
                node.next = l1
                l1 = l1!.next
            } else {
                node.next = l2
                l2 = l2!.next
            }
            
            node = node.next!
        }
        
        if l1 != nil {
            node.next = l1
        } else {
            node.next = l2
        }
        
        return dummy.next
    }
}<commit_msg>[LinkedList] Optimize code style for Merge K Sorted Lists<commit_after>/**
 * Question Link: https://leetcode.com/problems/merge-k-sorted-lists/
 * Primary idea: Divide and Conqure with merge two sorted lists as a helper function
 * Time Complexity: O(mlogn), m stands for the length of one list, n stands for the number of lists
 *                  Space Complexity: O(1)
 *
 * Definition for singly-linked list.
 * public class ListNode {
 *     public var val: Int
 *     public var next: ListNode?
 *     public init(_ val: Int) {
 *         self.val = val
 *         self.next = nil
 *     }
 * }
 */

class MergeKSortedLists {
    func mergeKLists(lists: [ListNode?]) -> ListNode? {
        guard lists.count > 0 else {
            return nil
        }
    
        var left = 0
        var right = lists.count - 1
        
        var lists = lists
        
        while right > 0 {
            left = 0
            while left < right {
                lists[left] = _mergeTwoLists(lists[left], lists[right])
                left += 1
                right -= 1
            }
        }
        
        return lists[0]
    }
    
    private func _mergeTwoLists(l1: ListNode?, _ l2: ListNode?) -> ListNode? {
        let dummy = ListNode(0)
        var node = dummy
    
        var l1 = l1
        var l2 = l2
        
        while l1 != nil && l2 != nil {
            if l1!.val < l2!.val {
                node.next = l1
                l1 = l1!.next
            } else {
                node.next = l2
                l2 = l2!.next
            }
            
            node = node.next!
        }
        
        node.next = l1 ?? l2
        
        return dummy.next
    }
}
{'fraction_non_alphanumeric': 0.09370424597364568, 'fraction_numerical': 0.016691068814055635, 'mean_word_length': 1.2181818181818183, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4020761', 'n_tokens_mistral': 1041, 'n_tokens_neox': 988, 'n_words': 301}