标签:ted sorted leetcode public wol 通过 lin 代码 turn
题目链接:https://leetcode-cn.com/problems/merge-two-sorted-lists/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode temp = head;
while(l1 != null && l2 != null){
if(l1.val < l2.val){
temp.next = l1; //此时l1指向temp的下一个
l1 = l1.next; //l1继续往下一步走
}else{
temp.next = l2; //此时l2指向temp的下一个
l2 = l2.next; //l2继续往下一步走
}
temp = temp.next; //temp继续向下一步走
}
temp.next = l1==null ? l2:l1; //谁还没空,谁就直接附着到temp上面
return head.next;
}
}
大家如果感兴趣可以前去手搓
本分类只用作个人记录,大佬轻喷.
标签:ted sorted leetcode public wol 通过 lin 代码 turn
原文地址:https://www.cnblogs.com/xiaofff/p/12813987.html