linkedlist.go
func merge(left, right *llNode, comparator Comparator) (first *llNode, err error) {
	curLeft := left
	curRight := right

	var last *llNode

	appendResults := func(updated *llNode) {
		if last == nil {
			last = updated
		} else {
			last.next = updated
			last = last.next
		}
		if first == nil {
			first = last
		}
	}

	for curLeft != nil && curRight != nil {
		var res int
		if res, err = comparator(curLeft.payload, curRight.payload); nil != err {
			break // Don't return, stitch the remaining elements back on.
		} else if res < 0 {
			appendResults(curLeft)
			curLeft = curLeft.next
		} else {
			appendResults(curRight)
			curRight = curRight.next
		}
	}

	if curLeft != nil {
		appendResults(curLeft)
	}
	if curRight != nil {
		appendResults(curRight)
	}
	return
}
func merge(left, right *llNode, comparator Comparator) (first *llNode, err error) {
	curLeft := left
	curRight := right

	var last *llNode

	appendResults := func(updated *llNode) {
		if last == nil {
			last = updated
		} else {
			last.next = updated
			last = last.next
		}
		if first == nil {
			first = last
		}
	}

	for curLeft != nil && curRight != nil {
		var res int
		if res, err = comparator(curLeft.payload, curRight.payload); nil != err {
			break // Don't return, stitch the remaining elements back on.
		} else if res < 0 {
			appendResults(curLeft)
			curLeft = curLeft.next
		} else {
			appendResults(curRight)
			curRight = curRight.next
		}
	}

	if curLeft != nil {
		appendResults(curLeft)
	}
	if curRight != nil {
		appendResults(curRight)
	}
	return
}
func merge ( left , right * llNode , comparator Comparator ) ( first * llNode , err error ) { curLeft := left curRight := right var last * llNode appendResults := func ( updated * llNode ) { if last == nil { last = updated } else { last . next = updated last = last . next } if first == nil { first = last } } for curLeft != nil && curRight != nil { var res int if res , err = comparator ( curLeft . payload , curRight . payload ) ; nil != err { break // Don't return, stitch the remaining elements back on. } else if res < 0 { appendResults ( curLeft ) curLeft = curLeft . next } else { appendResults ( curRight ) curRight = curRight . next } } if curLeft != nil { appendResults ( curLeft ) } if curRight != nil { appendResults ( curRight ) } return }
// merge takes two sorted lists and merges them into one sorted list.
// Behavior is undefined when you pass a non-sorted list as `left` or `right`
merge takes two sorted lists and merges them into one sorted list . Behavior is undefined when you pass a non - sorted list as left or right