leetcode_merge_two_sorted_lists 发表于 2016-11-29 难度:Easy 注意:new一个ListNode,返回的是ListNode的指针;直接调用ListNode构造函数,返回的是ListNode。代码如下: 123456789101112131415161718192021222324252627282930313233343536373839/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* dummy = new ListNode(-1); ListNode* cur = dummy; while(l1 != NULL && l2 != NULL) { if(l1->val < l2->val) { cur->next = l1; cur = cur->next; l1 = l1->next; } else { cur->next = l2; cur = cur->next; l2 = l2->next; } } if(l1 != NULL) { cur->next = l1; } if(l2 != NULL) { cur->next = l2; } return dummy->next; }}; 运行结果:9ms,超过23.48%