Merge Sorted Lists
Merge sorted linked lists and return it as a new list. The new list should is made by splicing together the nodes of the first of the lists.
Show Tags
Solution 1:
Use Dummynode to record the first node of the head, easy to complete, 2 minutes on AC!
1 /**2 * Definition for singly-linked list.3 * public class ListNode {4 * int val;5 * ListNode Next;6 * ListNode (int x) {7 * val = x;8 * next = null;9 * }Ten * } One */ A Public classSolution { - PublicListNode mergetwolists (listnode L1, ListNode L2) { -ListNode dummy =NewListNode (0); theListNode cur =dummy; - - while(L1! =NULL&& L2! =NULL) { - if(L1.val <l2.val) { +Cur.next =L1; -L1 =L1.next; +}Else { ACur.next =L2; atL2 =L2.next; - } -Cur =Cur.next; - } - - if(L1! =NULL) { inCur.next =L1; -}Else { toCur.next =< + } - the returnDummy.next; * } $}View Code
GITHUB:
Https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/list/MergeTwoLists_1206.java
Leetcode:merge-Sorted Lists Problem Solving report