Leetcode 21. Merge Two Sorted Lists

来源:互联网 发布:基金推荐知乎 编辑:程序博客网 时间:2024/06/06 03:43

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.

s思路:
1. 常规题。就是考察对list的操作,需要边比较边根据比较的结果来觉得移动两个list中的那一个的指针。

//方法1:用recursive的方法来比较class Solution {public:    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {        if(!l1) return l2;        if(!l2) return l1;        if(l1->val>l2->val){            l2->next=mergeTwoLists(l1,l2->next);            return l2;        }else{            l1->next=mergeTwoLists(l1->next,l2);            return l1;                  }           }};
0 0