Albert處理文字分類任務
阿新 • • 發佈:2020-12-13
輸入兩個遞增排序的連結串列,合併這兩個連結串列並使新連結串列中的節點仍然是遞增排序的。
示例1:
輸入:1->2->4, 1->3->4
輸出:1->1->2->3->4->4
限制:
0 <= 連結串列長度 <= 1000
/** * 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 p = new ListNode(-1), pre = p; while (l1 != null && l2 != null) { if(l1.val <= l2.val) { pre.next = l1; pre = pre.next; l1 = l1.next; }else{ pre.next = l2; pre = pre.next; l2 = l2.next; } } if (l1 != null) { pre.next = l1; } if (l2 != null) { pre.next = l2; } return p.next; } }