leetcode练习:21. Merge Two Sorted Lists
Posted 年糕君の勉强笔记
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode练习:21. Merge Two Sorted Lists相关的知识,希望对你有一定的参考价值。
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
var mergeTwoLists = function(l1, l2) { var p = l1; var q = l2; var k = null; var head = null; if(p == null && q == null) return null; if(p == null ) return q; if(q == null ) return p; while(p!=null && q!=null){ if(head == null) { if(p.val <= q.val) { head = p; p = p.next; } else { head = q; q = q.next; } k = head; } else { if(p.val <= q.val) { k.next = p; p = p.next; } else { k.next = q; q = q.next; } k = k.next; } } if(p!=null) { k.next = p; } if(q!=null) { k.next = q; } return head; };
以上是关于leetcode练习:21. Merge Two Sorted Lists的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 21. Merge Two Sorted Lists
Leetcode---21. Merge Two Sorted Lists
Leetcode 21. Merge Two Sorted Lists
LeetCode 21. Merge Two Sorted Lists