Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
我的代码:
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
List<Integer> list = new LinkedList<>();
while (l1 != null) {
list.add(l1.val);
l1 = l1.next;
}
while (l2 != null) {
list.add(l2.val);
l2 = l2.next;
}
Collections.sort(list);
if (list.size() == 0)
return null;
ListNode listNode = new ListNode(list.get(0));
ListNode answer = listNode;
for (int i = 1; i < list.size(); i++) {
listNode.next = new ListNode(list.get(i));
listNode = listNode.next;
}
return answer;
}