力扣21
https://leetcode-cn.com/problems/merge-two-sorted-lists/
123456789101112131415161718
public ListNode mergeTwoLists(ListNode l1, ListNode l2) { //虚拟头部 ListNode root = new ListNode(), cur = root; //先合并公共长度部分 while (l1 != null && l2 != null){ if (l1.val <= l2.val) { cur.next = l1; l1 = l1.next; }else { cur.next = l2; l2 = l2.next; } cur = cur.next; } //剩下的直接接上 cur.next = (l1 == null) ? l2 : l1; return root.next;}
其他链表类型的题目点击这里
wjwABCDEFG
Shaoguan, China
文章
102
分类
14
标签
26
游戏和引擎
生活随笔
数据结构和算法
Update your browser to view this website correctly. Update my browser now
×