博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Merge Two Sorted Lists_LeetCode
阅读量:4512 次
发布时间:2019-06-08

本文共 746 字,大约阅读时间需要 2 分钟。

Description:

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.

 

解题思路:

分条件判断,利用递归

 

Code:

/** * 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) {        if(l1 == NULL) return l2;        if(l2 == NULL) return l1;                if(l1->val < l2->val) {            l1->next = mergeTwoLists(l1->next, l2);            return l1;        } else {            l2->next = mergeTwoLists(l2->next, l1);            return l2;        }    }};

 

转载于:https://www.cnblogs.com/SYSU-Bango/p/7751846.html

你可能感兴趣的文章
Turn the corner (三分)
查看>>
srs2录制flv文件metadata不准确
查看>>
P2512 [HAOI2008]糖果传递
查看>>
P3224 [HNOI2012]永无乡(平衡树合并)
查看>>
sql2000远程连接失败
查看>>
substring(start,stop)用于提取字符串中介于两个指定下标之间的字符。
查看>>
第三百零六天 how can I 坚持
查看>>
WEB Fuzz中需要关注的7种响应
查看>>
§1.2.1绝对值不等式第10题第二小问
查看>>
dip
查看>>
利用INI文件实现界面无闪烁多语言切换
查看>>
sql语句的一种组织方法
查看>>
最精简的IOCP封装
查看>>
Jmeter-JDBC连接测试
查看>>
HDU 4344
查看>>
nyoj43 24 Point game(DFS)
查看>>
RedisTemplate操作Redis
查看>>
Android基础TOP5_2:MultiAutoCompleteTextView多文本自动补全文本框
查看>>
初识NodeJS,一个基于GoogleV8引擎的Javascript运行环境
查看>>
Selenium—获取页面的title,url;使用句柄方式切换窗口
查看>>