Merge two sorted linked lists

Sort by

recency

|

23 Discussions

|

  • + 0 comments

    Java

    static SinglyLinkedListNode mergeLists(SinglyLinkedListNode head1, SinglyLinkedListNode head2) {
        SinglyLinkedListNode mergedHead = new SinglyLinkedListNode(0);
        SinglyLinkedListNode current = mergedHead;
    
        while (head1 != null && head2 != null) {
            if (head1.data <= head2.data) {
                current.next = head1;
                head1 = head1.next;
            } else {
                current.next = head2;
                head2 = head2.next;
            }
            current = current.next;
        }
    
        current.next = (head1 != null) ? head1 : head2;
    
        return mergedHead.next; 
    }
    
  • + 0 comments

    Java with some sorting

    List<SinglyLinkedListNode> l = new ArrayList<>();
    while (head1 != null || head2 != null) {
    	if (head1 != null) {
    		l.add(head1);
    	}
    	if (head2 != null) {
    		l.add(head2);
    	}
    	head1 = head1 != null ? head1.next : null;
    	head2 = head2 != null ? head2.next : null;
    }
    l = l.stream()
    	.sorted(Comparator.comparingInt(n -> n.data))
    	.collect(java.util.stream.Collectors.toList());
    for (int i = 0; i < l.size() - 1; i++) {
    	SinglyLinkedListNode c = l.get(i);
    	c.next = l.get(i+1);
    }
    return l.get(0);
    
  • + 0 comments

    Python3 that would have been more efficient if _ _next__was implemented

    def mergeLists(head1, head2):
        node = head1
        node2 = head2
    		arr = []
        while (node is not None):
            arr.append(node.data)
            node = node.next
        while (node2 is not None):
            arr.append(node2.data)
            node2= node2.next
       
    
    arr.sort()
    sortedSLL=SinglyLinkedList()    
    for i in arr:
        sortedSLL.insert_node(i)
    return sortedSLL.head
    
  • + 0 comments

    JS

    function mergeLists(head1, head2) {
        const list = [];
        while (head1) {
            list.push(head1.data);
            head1 = head1.next;
        }
        while (head2) {
            list.push(head2.data);
            head2 = head2.next;
        }
        list.sort((a, b) => a - b);
        const head = new SinglyLinkedListNode(list[0]);
        list.reduce((node, value, index) => {
            if (index) {
                node.next = new SinglyLinkedListNode(value);
                node = node.next;
            }
            return node;
        }, head);
        return head;
    }
    
  • + 0 comments

    C#

        static SinglyLinkedListNode mergeLists(SinglyLinkedListNode head1, SinglyLinkedListNode head2)
        {
            if (head1 == null) return head2;
            if (head2 == null) return head1;
            SinglyLinkedListNode head;
            if (head1.data <= head2.data)
            {
                head = head1;
                head1 = head1.next;
            }
            else
            {
                head = head2;
                head2 = head2.next;
            }
            head.next = mergeLists(head1, head2);
            return head;
        }