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 ...
随机推荐
- 安装字体或直接调用非注册字体 z
1.安装字体//程序直接将字体文件安装的系统中.函数声明:[DllImport("kernel32.dll", SetLastError = true)] static exter ...
- IOS GCDAsyncSocket
// // ViewController.m // 05.聊天室 // // Created by apple on 14/12/5. // Copyright (c) 2014年 heima. Al ...
- mobile easyui兼容实体数据(tree插件为例)
ORM的实体类和数据库的类是一一对应的,如果有多级的嵌套循环json返回到前台为了方便展示可以使用mobile easyui,但是mobile easyui又需要特定的属性才可以,比如id,text, ...
- CodeForces 30C Shooting Gallery 简单dp
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qq574857122/article/details/36944227 题目链接:点击打开链接 给定 ...
- Gym - 101334F 单调栈
当时我的第一想法也是用单调栈,但是被我写炸了:我也不知道错在哪里: 看了大神的写法,用数组模拟的: 记录下单调递增栈的下标,以及每个数字作为最小值的最左边的位置. 当有数据要出栈的时候,说明栈里的数据 ...
- 「SDOI2008沙拉公主的困惑」
题目 看着有点可怕 求 \[\sum_{i=1}^{n!}[(i,m!)=1]\] 考虑一下\(m=n\)的时候的答案 非常显然就是\(\varphi(m!)\) 而如果\(n>m\) 非常显然 ...
- java 线程状态图
- Android学习笔记_22_服务Service应用之—与Activity进行相互通信的本地服务
一.启动服务的两种方法方法: 第一种: startService()和stopService()启动关闭服务.适用于服务和Activity之间没有调用交互的情况.如果相互之间需要方法调用或者传递参数 ...
- C# 动态改变webservice的访问地址
1.添加一个App.config配置文件. 2.配置服务http://Lenovo-PC:80/EvisaWS/WharfService?wsdl,那么在上面的文件中就会自动生成服务的配置: < ...
- linux网络相关配制及命令
1.虚拟机配制 查看ip: ip addr 配制网卡(读者可以忽略): 编辑虚拟网络编辑器,修改子网IP 查看ip,输入ip addr 开启网络:ifup eth0 关闭网络:ifdown eth0 ...