20.json 6.3 KB
Newer Older
每日一练社区's avatar
test  
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
{
   "question_id": 21,
   "question_title": "合并两个有序链表",
   "difficulty": "简单",
   "question_content": "<p>将两个升序链表合并为一个新的 <strong>升序</strong> 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 </p><p> </p><p><strong>示例 1:</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0021.Merge%20Two%20Sorted%20Lists/images/merge_ex1.jpg\" style=\"width: 662px; height: 302px;\" /><pre><strong>输入:</strong>l1 = [1,2,4], l2 = [1,3,4]<strong><br />输出:</strong>[1,1,2,3,4,4]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>l1 = [], l2 = []<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>l1 = [], l2 = [0]<strong><br />输出:</strong>[0]</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li>两个链表的节点数目范围是 <code>[0, 50]</code></li>\t<li><code>-100 <= Node.val <= 100</code></li>\t<li><code>l1</code> 和 <code>l2</code> 均按 <strong>非递减顺序</strong> 排列</li></ul>",
   "topic_link": "https://bbs.csdn.net/topics/600469819",
   "cpp": "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};",
   "java": "public class ListNode {\n\tint val;\n\tListNode next;\n\tListNode() {\n\t}\n\tListNode(int val) {\n\t\tthis.val = val;\n\t}\n\tListNode(int val, ListNode next) {\n\t\tthis.val = val;\n\t\tthis.next = next;\n\t}\n}\nclass Solution {\n\tpublic ListNode mergeTwoLists(ListNode l1, ListNode l2) {\n\t\tListNode h = new ListNode(0, null);\n\t\tListNode p = h;\n\t\twhile (l1 != null && l2 != null) {\n\t\t\tif (l1.val < l2.val) {\n\t\t\t\tp.next = l1;\n\t\t\t\tp = l1;\n\t\t\t\tl1 = l1.next;\n\t\t\t} else {\n\t\t\t\tp.next = l2;\n\t\t\t\tp = l2;\n\t\t\t\tl2 = l2.next;\n\t\t\t}\n\t\t}\n\t\tif (l1 != null) {\n\t\t\tp.next = l1;\n\t\t} else {\n\t\t\tp.next = l2;\n\t\t}\n\t\treturn h.next;\n\t}\n}",
   "js": "/**\n * Definition for singly-linked list.\n * function ListNode(val) {\n *     this.val = val;\n *     this.next = null;\n * }\n */\n/**\n * @param {ListNode} l1\n * @param {ListNode} l2\n * @return {ListNode}\n */\nvar mergeTwoLists = function(l1, l2) {\n    let v1=l1;\n    let v2=l2;\n    let v1pre = {val:0,next:v1};\n    let root = v1pre;\n    let d;\n\n    const slide = (pre, node, target)=>{\n\n    \tif(node.val>target){\n    \t\treturn [1, pre, node];\n    \t}\n\n    \twhile(node.next!=null){\n    \t\tif(node.next.val>=target){\n    \t\t\treturn [0, pre, node]\n    \t\t}else{\n    \t\t\tnode = node.next;\n    \t\t\tpre = pre.next;\n    \t\t}\n    \t}\n    \treturn [0, pre, node];\n    }\n\n    while(true){\n    \tif(v1!=null&&v2!=null){\n    \t\t[d, v1pre, v1] = slide(v1pre, v1, v2.val);\n    \t\tif(d===0){\n    \t\t\tv1.next = {\n\t    \t\t\tval: v2.val,\n\t    \t\t\tnext: v1.next\n\t    \t\t};\n    \t\t}else{\n    \t\t\tv1pre.next = {\n\t    \t\t\tval: v2.val,\n\t    \t\t\tnext: v1\n\t    \t\t};\n\t    \t\tv1 = v1pre.next;\n    \t\t}\n    \t\t\n    \t\tv2=v2.next;\n    \t}else if(v1!=null&&v2==null){\n    \t\tbreak;\n    \t}else if(v1==null&&v2!=null){\n    \t\tv1pre.next=v2;\n    \t\tbreak;\n    \t}else{\n    \t\tbreak;\n    \t}\n    }\n\n    // console.log(v1pre.next);\n\n    return root.next;\n};\n\nfunction main(){\n\tconst testCases = [\n\t\t[\n\t\t\tnull,\n\t\t\t{\"val\":0,\"next\":null},\n\t\t\t{\"val\":0,\"next\":null},\n\t\t],\n\n\t\t[\n\t\t\t{\"val\":2,\"next\":null},\n\t\t\t{\"val\":1,\"next\":null},\n\t\t\t{val:1,next:{val:2,next:null}}\n\t\t],\n\n\t\t[\n\t\t\t{\"val\":2,\"next\":null},\n\t\t\t{\"val\":2,\"next\":{val:1}},\n\t\t\t{val:1,next:{val:2,next:{val:2,next:null}}}\n\t\t],\n\n\t\t[\n\t\t\t{val:1,next:{val:2,next:{val:4}}},\n\t\t\t{val:1,next:{val:3,next:{val:4}}},\n\t\t\t{val:1,next:{val:1,next:{val:2,next:{val:3,next:{val:4,next:{val:4}}}}}}\n\t\t],\n\n\t\t[\n\t\t\t{val: 5},\n\t\t\t{val:1,next:{val:2,next:{val:4}}},\n\t\t\t{val:1,next:{val:2,next:{val:4,next:{val:5}}}},\n\t\t]\n\t];\n\tfor(const testCase of testCases){\n\t\tconst v = mergeTwoLists(testCase[0], testCase[1]);\n\t\tif(JSON.stringify(v)===JSON.stringify(testCase[2])){\n\t\t\tconsole.log(`[OK]`);\n\t\t}else{\n\t\t\tconsole.log(`[ERROR]`);\n\t\t}\n\t}\n}\n\nmain();\n\n\n\n\n\n",
   "python": "class ListNode:\n\tdef __init__(self, val=0, next=None):\n\t\tself.val = val\n\t\tself.next = next\nclass LinkList:\n\tdef __init__(self):\n\t\tself.head=None\n\tdef initList(self, data):\n\t\tself.head = ListNode(data[0])\n\t\tr=self.head\n\t\tp = self.head\n\t\tfor i in data[1:]:\n\t\t\tnode = ListNode(i)\n\t\t\tp.next = node\n\t\t\tp = p.next\n\t\treturn r\n\tdef\tconvert_list(self,head):\n\t\tret = []\n\t\tif head == None:\n\t\t\treturn\n\t\tnode = head\n\t\twhile node != None:\n\t\t\tret.append(node.val)\n\t\t\tnode = node.next\n\t\treturn ret\nclass Solution:\n\tdef mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:\n\t\th = ListNode(0, None)\n\t\tp = h\n\t\twhile l1 and l2:\n\t\t\tif l1.val < l2.val:\n\t\t\t\tp.next = l1\n\t\t\t\tp = l1\n\t\t\t\tl1 = l1.next\n\t\t\telse:\n\t\t\t\tp.next = l2\n\t\t\t\tp = l2\n\t\t\t\tl2 = l2.next\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# %%\nl = LinkList()\nlist1 = [1,2,4]\nlist2 = [1,3,4]\nl1 = l.initList(list1)\nl2 = l.initList(list2)\ns = Solution()\nprint(l.convert_list(s.mergeTwoLists(l1, l2)))",
   "status": 1,
   "keywords": "递归,链表",
   "license": {
      "cpp": "csdn.net",
      "python": "csdn.net",
      "java": "csdn.net"
   },
   "notebook": {
      "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/20/20_cpp.ipynb?type=file",
      "python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/20/20_python.ipynb?type=file",
      "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/20/20_java.ipynb?type=file"
   },
   "notebook_enable": 1
}