Leecode刷题之旅-C语言/python-21.合并两个有序链表
/*
* @lc app=leetcode.cn id=21 lang=c
*
* [21] 合并两个有序链表
*
* https://leetcode-cn.com/problems/merge-two-sorted-lists/description/
*
* algorithms
* Easy (52.72%)
* Total Accepted: 47.1K
* Total Submissions: 89K
* Testcase Example: '[1,2,4]\n[1,3,4]'
*
* 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
*
* 示例:
*
* 输入:1->2->4, 1->3->4
* 输出:1->1->2->3->4->4
*
*
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { struct ListNode* newNode;
if(!l1)
return l2;
if(!l2)
return l1;
if(l1->val<l2->val)
{
newNode=l1;
newNode->next=mergeTwoLists(l1->next,l2);
}
else
{
newNode=l2;
newNode->next=mergeTwoLists(l1,l2->next);
} return newNode;
}
这里用递归的方法进行合并。在第一次的判断中, 如果l1的值小于l2的值,新的结点从l1开始,然后下一个结点 继续是 l1的下一个值和l2进行该函数比较,反之则从l2开始。
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
python:
#
# @lc app=leetcode.cn id=21 lang=python3
#
# [21] 合并两个有序链表
#
# https://leetcode-cn.com/problems/merge-two-sorted-lists/description/
#
# algorithms
# Easy (52.72%)
# Total Accepted: 47.1K
# Total Submissions: 89K
# Testcase Example: '[1,2,4]\n[1,3,4]'
#
# 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
#
# 示例:
#
# 输入:1->2->4, 1->3->4
# 输出:1->1->2->3->4->4
#
#
#
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if l1==None and l2==None:
return None
if l1==None:
return l2
if l2==None:
return l1
if l1.val<=l2.val:
l1.next=self.mergeTwoLists(l1.next,l2)
return l1
else:
l2.next=self.mergeTwoLists(l1,l2.next)
return l2
Leecode刷题之旅-C语言/python-21.合并两个有序链表的更多相关文章
- Leecode刷题之旅-C语言/python-88合并两个有序数组
/* * @lc app=leetcode.cn id=88 lang=c * * [88] 合并两个有序数组 * * https://leetcode-cn.com/problems/merge-s ...
- python刷LeetCode:21. 合并两个有序链表
难度等级:简单 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4输出:1 ...
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
随机推荐
- Flask入门 flask结构 url_for 重定向(一)
Flask入门(一) 1 安装虚拟环境Mac,linux sudo pip install virtualenv ubuntu系统 sudo apt-get install python-virt ...
- java对文件操作--01
1.删除文件 /** * delete file * 删除文件 * @param fileName * @return */ private boolean deleteDir(String file ...
- escape,unescape与encodeURIComponent,decodeURIComponent
escape:将string转成unicode字符串 escape('
- OC extern和变量
注意: extern只能用来声明全部变量,不能拿来定义变量 #include <stdio.h> // 第一种做法是将a定义在main函数的前面 // int a; // 完整地声明全部变 ...
- libevent evbuffer参考文章
https://blog.csdn.net/FreeeLinux/article/details/52799951 http://cache.baiducontent.com/c?m=9d78d513 ...
- 2016-2017 ACM Central Region of Russia Quarterfinal Programming Contest
2016-2017 ACM Central Region of Russia Quarterfinal Programming Contest A. Fried Fish 题意:有N条鱼,有一个同时可 ...
- html基本代码书写
html的书写: datagrid---->toolbar---->按钮---->handler:function() var rows=$("gridId"). ...
- HBuilder实现WiFi调试Android
要求手机是开发模式 wifi实现 条件:已ROOT手机.手机和电脑需要在一个网段 第一步:安装在应用商店下载WiFi ADB (注意这里显示的ip等下使用) 第二步:打开WIFI ADB 第三步:切换 ...
- 【luogu P2065 [TJOI2011]卡片】 假题解
题目链接:https://www.luogu.org/problemnew/show/P2065 辣鸡匈牙利,没有优化贼鸡儿慢 // luogu-judger-enable-o2 #include & ...
- 用java语言编写的简单二叉树
package com.cjonline.foundation.evisa; public class TestTree { private int data=-1; private TestTree ...