https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337

非递归

之前好像在《算法笔记》上做过,是某种排序算法思想?

  1. public ListNode Merge(ListNode list1, ListNode list2) {
  2. ListNode head = new ListNode(0);
  3. ListNode node = head;
  4. while (list1 != null && list2 != null) {
  5. if (list1.val <= list2.val) {
  6. node.next = list1;
  7. node = list1;
  8. list1 = list1.next;
  9. } else {
  10. node.next = list2;
  11. node = list2;
  12. list2 = list2.next;
  13. }
  14. }
  15. while (list2 != null) {
  16. node.next = list2;
  17. node = list2;
  18. list2 = list2.next;
  19. }
  20. while (list1 != null) {
  21. node.next = list1;
  22. node = list1;
  23. list1 = list1.next;
  24. }
  25. return head.next;
  26. }