Untitled
unknown
plain_text
2 years ago
1.7 kB
1
Indexable
Never
func mergeTwoLists(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? { var finalList: ListNode? var firstList: ListNode? = list1 var secondList: ListNode? = list2 var tmpListNode: ListNode? var cnt = 0 while(firstList != nil && secondList != nil) { if firstList!.val <= secondList!.val { if cnt==0 { tmpListNode = firstList finalList = tmpListNode }else { tmpListNode!.next = firstList tmpListNode = firstList } firstList = firstList!.next }else { if cnt==0 { tmpListNode = secondList finalList = tmpListNode }else { tmpListNode!.next = secondList tmpListNode = secondList } secondList = secondList!.next } cnt += 1 } while(firstList != nil) { if cnt==0 { tmpListNode = firstList finalList = tmpListNode }else { tmpListNode!.next = firstList tmpListNode = firstList } firstList = firstList!.next cnt += 1 } while(secondList != nil) { if cnt==0 { tmpListNode = secondList finalList = tmpListNode }else { tmpListNode!.next = secondList tmpListNode = secondList } secondList = secondList!.next cnt += 1 } return finalList }