复习-JZ16 合并两个排序的链表
合并两个排序的链表
https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337?tpId=13&&tqId=11169&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
2021年9月12日08:53:15
2021年9月12日09:00:57
/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ public class Solution { public ListNode Merge(ListNode list1,ListNode list2) { ListNode cur1 = list1; ListNode cur2 = list2; ListNode head = new ListNode(-1); ListNode cur3 = head; while(cur1 != null && cur2 != null){ if(cur1.val > cur2.val){ cur3.next = new ListNode(cur2.val); cur3 = cur3.next; cur2 = cur2.next; } else{ cur3.next = new ListNode(cur1.val); cur3 = cur3.next; cur1 = cur1.next; } } while(cur1 != null){ cur3.next = new ListNode(cur1.val); cur3 = cur3.next; cur1 = cur1.next; } while(cur2 != null){ cur3.next = new ListNode(cur2.val); cur3 = cur3.next; cur2 = cur2.next; } return head.next; } }