You are viewing a single comment's thread. Return to all comments →
C#
static SinglyLinkedListNode mergeLists(SinglyLinkedListNode head1, SinglyLinkedListNode head2) { var queue = new Queue<SinglyLinkedListNode>(); while (head1 != null && head2 != null) { if (head1.data < head2.data) { queue.Enqueue(head1); head1 = head1.next; continue; } queue.Enqueue(head2); head2 = head2.next; } var head = queue.Dequeue(); var current = head; while (queue.Count > 0) { current.next = queue.Dequeue(); current = current.next; } current.next = head1 != null ? head1 : head2; return head; }
Seems like cookies are disabled on this browser, please enable them to open this website
Merge two sorted linked lists
You are viewing a single comment's thread. Return to all comments →
C#