合并两个有序链表
No more than a platitude
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.
Example:
Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4
Mine
class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode head = new ListNode(0);// 虚表头 ListNode point = head; while (l1 != null && l2 != null) { if(l1.val <= l2.val){ // 不断让表接上头 point.next = l1; point = point.next; l1 = l1.next; } else { point.next = l2; point = point.next; l2 = l2.next; } } if (l1 == null) { point.next = l2; } else { point.next = l1; } return head.next; } }
我开始是准备重置两个指针互相捣腾。然后返回最原先设定的那个表头。但是做了半天发现没有办法解决有连续数字的问题,然后就是这种重新建两个指针,让本来的表头互相捣腾。方便多了。