Merge two sorted linked lists

Sort by

recency

|

25 Discussions

|

  • + 0 comments

    I'm actually just gonna post every time I see these uneditable classes that don't compile. Srsly! (C#)

    Super easy in python :(

    def mergeLists(head1, head2):
        head = SinglyLinkedListNode(0)
        end = head
        while head1 is not None or head2 is not None:
            if head1 is None or (head2 is not None and head1.data > head2.data):
                (head1, head2) = (head2, head1)
            end.next = head1
            head1 = head1.next
            end = end.next
        return head.next 
    
  • + 0 comments

    My Python Solution

    def mergeLists(head1, head2):
        
        dummy = SinglyLinkedListNode(0)
        current = dummy
        
        while head1 and head2:
            
            if head1.data <= head2.data:
                current.next = head1
                head1 = head1.next
            else:
                current.next = head2
                head2 = head2.next
                
            current = current.next
    
        if head1:
            current.next = head1
        if head2:
            current.next = head2
    
        return dummy.next
    
  • + 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