/* 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 head = new ListNode(Integer.MIN_VALUE); //当链表完成合并后head不再指向原地址,所以用curr存储原地址 ListNode curr = head; //遍历循环大小,排序 while (list1 != null && list2 != null) { if (list1.val <= list2.val) { head.next = list1; list1 = list1.next; } else { head.next = list2; list2 = list2.next; } head = head.next; } if (list1 != null) head.next = list1; if (list2 != null) head.next = list2; //从链表要返回原地址的next,才是真正的排序好的新链表 return curr.next; } }