原题描述

原题链接

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

示例 1:

输入:l1 = [1,2,4], l2 = [1,3,4] 输出:[1,1,2,3,4,4]

示例 2:

输入:l1 = [], l2 = [] 输出:[]

示例 3:

输入:l1 = [], l2 = [0] 输出:[0]


提示:

两个链表的节点数目范围是 [0, 50] -100 <= Node.val <= 100 l1 和 l2 均按 非递减顺序 排列

个人解法

Java(顺序插入)

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode() {}
  7. * ListNode(int val) { this.val = val; }
  8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  9. * }
  10. */
  11. public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
  12. ListNode p=list2;
  13. ListNode head=new ListNode(-1,list1);
  14. ListNode q=head;
  15. if (list1==null){
  16. return list2;
  17. }
  18. while (list1!=null){
  19. if(list2!=null&&list2.val> list1.val){
  20. list1=list1.next;
  21. q=q.next;
  22. }
  23. if (list1==null&&list2!=null){
  24. q.next=list2;
  25. break;
  26. }
  27. if (list2!=null&&list2.val== list1.val){
  28. if (list2.next!=null){
  29. p=list2.next;
  30. }else {
  31. p=null;
  32. }
  33. list2.next=list1.next;
  34. list1.next=list2;
  35. list2=p;
  36. list1=list1.next;
  37. q=q.next;
  38. }
  39. if (list2!=null&&list2.val<list1.val){
  40. //2 3 4 4 3
  41. if (list2.next!=null){
  42. p=list2.next;
  43. }else {
  44. p=null;
  45. }
  46. list2.next=list1;
  47. if (head.next==list1){
  48. head.next=list2;
  49. }else {
  50. q.next=list2;
  51. }
  52. list1=list2;
  53. list2=p;
  54. }
  55. if (list2==null){
  56. break;
  57. }
  58. }
  59. // if (list2!=null){
  60. // list1.next=list2;
  61. // }
  62. return head.next;
  63. }

JavaScript

  1. /**
  2. * @param {ListNode} l1
  3. * @param {ListNode} l2
  4. * @return {ListNode}
  5. */
  6. var mergeTwoLists = function (l1, l2) {
  7. if (l1 === null) {
  8. return l2;
  9. }
  10. if (l2 === null) {
  11. return l1;
  12. }
  13. if (l1.val < l2.val) {
  14. l1.next = mergeTwoLists(l1.next, l2);
  15. return l1;
  16. } else {
  17. l2.next = mergeTwoLists(l1, l2.next);
  18. return l2;
  19. }
  20. };

更优解法

Java

递归

  1. public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
  2. if (l1 == null) {
  3. return l2;
  4. } else if (l2 == null) {
  5. return l1;
  6. } else if (l1.val < l2.val) {
  7. l1.next = mergeTwoLists(l1.next, l2);
  8. return l1;
  9. } else {
  10. l2.next = mergeTwoLists(l1, l2.next);
  11. return l2;
  12. }
  13. }

迭代

  1. public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
  2. ListNode prehead = new ListNode(-1);
  3. ListNode prev = prehead;
  4. while (l1 != null && l2 != null) {
  5. if (l1.val <= l2.val) {
  6. prev.next = l1;
  7. l1 = l1.next;
  8. } else {
  9. prev.next = l2;
  10. l2 = l2.next;
  11. }
  12. prev = prev.next;
  13. }
  14. // 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可
  15. prev.next = l1 == null ? l2 : l1;
  16. return prehead.next;
  17. }

JavaScript

题解链接

  1. var mergeTwoLists = function(l1, l2) {
  2. const prehead = new ListNode(-1);
  3. let prev = prehead;
  4. while (l1 != null && l2 != null) {
  5. if (l1.val <= l2.val) {
  6. prev.next = l1;
  7. l1 = l1.next;
  8. } else {
  9. prev.next = l2;
  10. l2 = l2.next;
  11. }
  12. prev = prev.next;
  13. }
  14. // 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可
  15. prev.next = l1 === null ? l2 : l1;
  16. return prehead.next;
  17. };