ES6 合并有序链表
// 方法一 function mergeTwoLists(l1, l2) { var l3 = new ListNode(-1); var c3 = l3; while (l1 !== null && l2 !== null) { if (l1.val <= l2.val) { c3.next = l1; l1 = l1.next; } else { c3.next = l2; l2 = l2.next; } c3 = c3.next; } c3.next = (l1 === null) ? l2 : l1; return l3.next; } // 自定义一个链表 function ListNode(val) { this.val = val; this.next = null; } // 方法二:递归 function mergeTwoList(l1, l2) { if (l1 = null && l2 == null) { return null; } if (l1 == null) { return l2; } if (l2 == null) { return l1; } if (l1.val < l2.val) { l1.next = mergeTwoList(l1.next, l2); return l1; } else { l2.next = mergeTwoList(l1, l2.next); return l2; } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论