Merge K Sorted Linked List

package org.wesome.dsalgo;

import lombok.Data;

import java.util.Objects;

public class LinkedList {
    public static Node mergeTwoSortedLists(Node nodeA, Node nodeB) {
        Node result;
        if (Objects.isNull(nodeA)) {
            return nodeB;
        } else if (Objects.isNull(nodeB)) {
            return nodeA;
        }
        if (nodeA.data <= nodeB.data) {
            result = nodeA;
            result.next = mergeTwoSortedLists(nodeA.next, nodeB);
        } else {
            result = nodeB;
            result.next = mergeTwoSortedLists(nodeA, nodeB.next);
        }
        return result;
    }

    public static Node mergeKSortedLists(Node arr[], int last) {
        while (last != 0) {
            int indx = 0, end = last;
            while (indx < end) {
                arr[indx] = mergeTwoSortedLists(arr[indx], arr[end]);
                indx++;
                end--;
                if (indx >= end)
                    last = end;
            }
        }
        return arr[0];
    }

    static void printIteratively(Node head) {
        System.out.println("LinkedList.printIteratively");
        if (Objects.isNull(head)) {
            System.out.println("Linked List is null = " + head);
            return;
        }
        Node tempNode = head;
        while (Objects.nonNull(tempNode)) {
            System.out.println("Node = " + tempNode.data);
            tempNode = tempNode.next;
        }
    }

    static void addInLast(Node head, int element) {
        System.out.println("\nLinkedList.addInLast " + element);
        Node newNode = new Node(element);
        if (Objects.isNull(head)) {
            head = newNode;
            return;
        }
        Node tempNode = head;
        while (Objects.nonNull(tempNode.next)) {
            tempNode = tempNode.next;
        }
        tempNode.next = newNode;
    }
}

@Data
class Node {
    int data;
    Node next;

    public Node(int data) {
        this.data = data;
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.wesome.dsalgo.LinkedList.addInLast;
import static org.wesome.dsalgo.LinkedList.mergeKSortedLists;
import static org.wesome.dsalgo.LinkedList.printIteratively;

public class LinkedListTest {
    @Test
    void mergeKSortedListsTest() {
        int linkedListCount = 3;

        Node arr[] = new Node[linkedListCount];

        Node list0 = new Node(2);
        addInLast(list0, 5);
        addInLast(list0, 8);
        addInLast(list0, 11);

        Node list1 = new Node(1);
        addInLast(list1, 4);
        addInLast(list1, 7);
        addInLast(list1, 10);

        Node list2 = new Node(0);
        addInLast(list2, 3);
        addInLast(list2, 6);
        addInLast(list2, 9);

        arr[0] = list0;
        arr[1] = list1;
        arr[2] = list2;

        Node head = mergeKSortedLists(arr, linkedListCount - 1);
        printIteratively(head);

        for (int i = 0; i < 11; i++) {
            Assertions.assertEquals(i, head.data);
            head = head.next;
        }
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.4.1"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on