牛客题霸-算法篇
反转链表
相似的企业真题
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 64M,其他语言128M
热度指数:1041008
本题知识点:
链表
算法知识视频讲解
校招时部分企业笔试将禁止编程题跳出页面,为提前适应,练习时请使用在线自测,而非本地IDE。
题目描述
输入一个链表,反转链表后,输出新链表的表头。
示例1
输入
复制
{1,2,3}
{1,2,3}
返回值
复制
{3,2,1}
{3,2,1}
说明:本题目包含复杂数据结构ListNode,
点此查看相关信息
下一题
登录
/
注册
我的提交
编辑器加载中...
/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ public class Solution { public ListNode ReverseList(ListNode head) { } }
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ class Solution { public: ListNode* ReverseList(ListNode* pHead) { } };
# -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: # 返回ListNode def ReverseList(self, pHead): # write code here
/* public class ListNode { public int val; public ListNode next; public ListNode (int x) { val = x; } }*/ class Solution { public ListNode ReverseList(ListNode pHead) { // write code here } }
/*function ListNode(x){ this.val = x; this.next = null; }*/ function ReverseList(pHead) { // write code here } module.exports = { ReverseList : ReverseList };
val = $x; } }*/ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param head ListNode类 * @return ListNode类 */ function ReverseList( $head ) { // write code here }
/*function ListNode(x){ this.val = x; this.next = null; }*/ function ReverseList(pHead) { // write code here }
# -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: # 返回ListNode def ReverseList(self, pHead): # write code here
package main import . "nc_tools" /* * type ListNode struct{ * Val int * Next *ListNode * } */ /** * * @param pHead ListNode类 * @return ListNode类 */ func ReverseList( pHead *ListNode ) *ListNode { // write code here }
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * * @param pHead ListNode类 * @return ListNode类 */ struct ListNode* ReverseList(struct ListNode* pHead ) { // write code here }
# class ListNode # attr_accessor :val, :next # # def initialize(val = 0, _next = nil) # @val, @next = val, _next # end # end # # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # @param head ListNode类 # @return ListNode类 # class Solution def ReverseList(head) # write code here end end
/** * class ListNode(var val: Int) { * var next: ListNode = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param head ListNode类 * @return ListNode类 */ def ReverseList(head: ListNode): ListNode = { // write code here } }
/** * class ListNode(var `val`: Int) { * var next: ListNode? = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param head ListNode类 * @return ListNode类 */ fun ReverseList(head: 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 head ListNode类 * @return ListNode类 */ public ListNode ReverseList (ListNode head) { // 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 head ListNode类 * @return ListNode类 */ export function ReverseList(head: 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 head ListNode类 * @return ListNode类 */ func ReverseList ( _ head: 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 head ListNode类 * @return ListNode类 */ pub fn ReverseList(&self, head: Option
>) -> Option
> { // write code here } }
{1,2,3}
{3,2,1}