剑指offer
合并两个排序的链表
相似的企业真题
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 64M,其他语言128M
热度指数:867134
本题知识点:
链表
算法知识视频讲解
校招时部分企业笔试将禁止编程题跳出页面,为提前适应,练习时请使用在线自测,而非本地IDE。
题目描述
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
示例1
输入
复制
{1,3,5},{2,4,6}
{1,3,5},{2,4,6}
返回值
复制
{1,2,3,4,5,6}
{1,2,3,4,5,6}
说明:本题目包含复杂数据结构ListNode,
点此查看相关信息
上一题
下一题
登录
/
注册
我的提交
编辑器加载中...
/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ public class Solution { public ListNode Merge(ListNode list1,ListNode list2) { } }
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ class Solution { public: ListNode* Merge(ListNode* pHead1, ListNode* pHead2) { } };
# -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: # 返回合并后列表 def Merge(self, pHead1, pHead2): # write code here
/* public class ListNode { public int val; public ListNode next; public ListNode (int x) { val = x; } }*/ class Solution { public ListNode Merge(ListNode pHead1, ListNode pHead2) { // write code here } }
/*function ListNode(x){ this.val = x; this.next = null; }*/ function Merge(pHead1, pHead2) { // write code here } module.exports = { Merge : Merge };
val = $x; } }*/ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param pHead1 ListNode类 * @param pHead2 ListNode类 * @return ListNode类 */ function Merge( $pHead1 , $pHead2 ) { // write code here }
/*function ListNode(x){ this.val = x; this.next = null; }*/ function Merge(pHead1, pHead2) { // write code here }
# -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: # 返回合并后列表 def Merge(self, pHead1, pHead2): # write code here
package main import . "nc_tools" /* * type ListNode struct{ * Val int * Next *ListNode * } */ /** * * @param pHead1 ListNode类 * @param pHead2 ListNode类 * @return ListNode类 */ func Merge( pHead1 *ListNode , pHead2 *ListNode ) *ListNode { // write code here }
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * * @param pHead1 ListNode类 * @param pHead2 ListNode类 * @return ListNode类 */ struct ListNode* Merge(struct ListNode* pHead1, struct ListNode* pHead2 ) { // write code here }
# class ListNode # attr_accessor :val, :next # # def initialize(val = 0, _next = nil) # @val, @next = val, _next # end # end # # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # @param pHead1 ListNode类 # @param pHead2 ListNode类 # @return ListNode类 # class Solution def Merge(pHead1, pHead2) # write code here end end
/** * class ListNode(var val: Int) { * var next: ListNode = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param pHead1 ListNode类 * @param pHead2 ListNode类 * @return ListNode类 */ def Merge(pHead1: ListNode,pHead2: ListNode): ListNode = { // write code here } }
/** * class ListNode(var `val`: Int) { * var next: ListNode? = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param pHead1 ListNode类 * @param pHead2 ListNode类 * @return ListNode类 */ fun Merge(pHead1: ListNode?,pHead2: ListNode?): ListNode? { // write code here } }
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param pHead1 ListNode类 * @param pHead2 ListNode类 * @return ListNode类 */ public ListNode Merge (ListNode pHead1, ListNode pHead2) { // write code here } }
/*class ListNode { * val: number * next: ListNode | null * constructor(val?: number, next?: ListNode | null) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param pHead1 ListNode类 * @param pHead2 ListNode类 * @return ListNode类 */ export function Merge(pHead1: ListNode, pHead2: ListNode): ListNode { // write code here }
/** * public class ListNode { * public var val: Int * public var next: ListNode? * public init(_ val: Int = 0, _ next: ListNode? = nil) { * self.val = val * self.next = next * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param pHead1 ListNode类 * @param pHead2 ListNode类 * @return ListNode类 */ func Merge ( _ pHead1: ListNode?, _ pHead2: ListNode?) -> ListNode? { // write code here } }
/** * #[derive(PartialEq, Eq, Debug, Clone)] * pub struct ListNode { * pub val: i32, * pub next: Option
> * } * * impl ListNode { * #[inline] * fn new(val: i32) -> Self { * ListNode { * val: val, * next: None, * } * } * } */ struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param pHead1 ListNode类 * @param pHead2 ListNode类 * @return ListNode类 */ pub fn Merge(&self, pHead1: Option
>, pHead2: Option
>) -> Option
> { // write code here } }
{1,3,5},{2,4,6}
{1,2,3,4,5,6}