{ "type": "code_options", "author": "csdn.net", "source": "solution.md", "exercise_id": "8cd00cecce4e49c591d9a8b83a7eed59", "keywords": "递归,链表", "title": "合并两个有序链表", "desc": [ { "content": "\n

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

 

示例 1:

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

示例 2:

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

示例 3:

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

 

提示:

", "language": "markdown" } ], "answer": [ { "content": "", "language": "cpp" } ], "prepared": [ [ { "content": "", "language": "cpp" } ], [ { "content": "", "language": "cpp" } ], [ { "content": "", "language": "cpp" } ] ], "template": { "content": "struct ListNode\n{\n\tint val;\n\tListNode *next;\n\tListNode(int x) : val(x), next(NULL) {}\n};\nclass Solution\n{\npublic:\n\tListNode *mergeTwoLists(ListNode *l1, ListNode *l2)\n\t{\n\t\tListNode h(0, nullptr);\n\t\tListNode *p = &h;\n\t\twhile (l1 && l2)\n\t\t{\n\t\t\tListNode **t;\n\t\t\tif (l1->val < l2->val)\n\t\t\t\tt = &l1;\n\t\t\telse\n\t\t\t\tt = &l2;\n\t\t\tp->next = *t;\n\t\t\tp = *t;\n\t\t\t*t = (*t)->next;\n\t\t}\n\t\tif (l1)\n\t\t\tp->next = l1;\n\t\telse\n\t\t\tp->next = l2;\n\t\treturn h.next;\n\t}\n};", "language": "cpp" }, "node_id": "dailycode-077c4588ce144b738c311ecd56b6c249", "license": "csdn.net", "created_at": 1637894160, "topic_link": "https://bbs.csdn.net/topics/600469819" }