题目1 .链表合起来(单链表使用归并,双向链表用快排)
https://leetcode.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) {
if(l1 == null || l2 ==null)return l1 ==null?l2:l1;
ListNode head = new ListNode(-1);
ListNode temp = head;
while(l1 !=null && l2 != null){
if(l1.val < l2 .val){
temp.next = l1;
l1 = l1.next;
}else{
temp.next = l2;
l2 = l2.next;
}
temp = temp.next;
temp.next = null;
}
temp.next = l1==null ?l2:l1;
return head.next;
}
}