You are viewing a single comment's thread. Return to all comments →
Scala solution:
def mergeList(list1: SinglyLinkedListNode, list2: SinglyLinkedListNode): SinglyLinkedListNode = { var a = list1 var b = list2 var retVal: SinglyLinkedListNode = null var tail: SinglyLinkedListNode = null def appendAndNext(origNode: SinglyLinkedListNode) = { val newNode = new SinglyLinkedListNode(origNode.data) if (retVal == null) retVal = newNode if (tail == null) tail = newNode else { tail.next = newNode tail = newNode } origNode.next } while (a != null && b != null) { if (a.data < b.data) a = appendAndNext(a) else b = appendAndNext(b) } while (a != null) a = appendAndNext(a) while (b != null) b = appendAndNext(b) retVal }
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 →
Scala solution: