题目链接:https://leetcode.com/problems/merge-two-sorted-lists/description/ 题目大意: 给出两个升序链表,将它们归并成一个链表,若有重复结点,都要链接上去,且新链表不新建结点. 法一:直接用数组归并的思想做,碰到一个归并一个,只是要注意链表前后结点之间的操作关系,应该弄清楚java里面引用之间的关系(此题容易面试当场写代码).代码如下(耗时14ms): /** * Definition for singly-linked list…
面试17题: 题目:打印从1到最大的n位数 题:输入数字n,按顺序打印出从1到最大的n位十进制数,比如输入3,则打印出1.2.3一直到最大的3位数999. 解题思路:需要考虑大数问题,这是题目设置的陷阱.可以把问题转换成数字排列问题,用递归让代码更简洁. 参见剑指offer P114 解题代码: # -*- coding:utf-8 -*- class Solution: def Print1ToMaxOfNDigits(self, n): if n<=0: return number=[']*…
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked lists and return it as a new list.The new list should be made by splicing together…
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists 需要排序!!! [2,4][1] 输出1 2 4 而不是2 4 1  递归版!! class Solution { public ListNode mergeTwo…
21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 解法一: /** * Definition for singly-linked list. * struct ListNode { * int val…
21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* dummy = ); ListNode* p = dummy; while(l1 && l2){ if(l1->val <= l2->val){ p->next = l1; p = p-&…
1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4-&g…
一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: Runtime: 20 ms, faster than 8.74% of C++ online submissions for Merge Two Sorted Lists. Memory Usage: 9.4 MB, less than 5.74% of C++ online submissions…
一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. (二)解题 这题是剑指offer上的老题了,剑指上面用的是递归,我写了个非递归的版本. /** * Definition for singly-linked list. *…
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 代…