BM1. 反转链表
描述
示例1
输入:
{1,2,3}
输出:
{3,2,1}
示例2
输入:
{}
输出:
{}
说明:
空链表则输出空Rust 解法, 执行用时: 2ms, 内存消耗: 328KB, 提交时间: 2021-12-15
/** * #[derive(PartialEq, Eq, Debug, Clone)] * pub struct ListNode { * pub val: i32, * pub next: Option<Box<ListNode>> * } * * 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<Box<ListNode>>) -> Option<Box<ListNode>> { // write code here let mut res=None; let mut cur=head; while let Some(mut x)=cur{ cur=x.next; x.next=res; res=Some(x); } res } }
Rust 解法, 执行用时: 2ms, 内存消耗: 340KB, 提交时间: 2021-09-19
/** * #[derive(PartialEq, Eq, Debug, Clone)] * pub struct ListNode { * pub val: i32, * pub next: Option<Box<ListNode>> * } * * 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<Box<ListNode>>) -> Option<Box<ListNode>> { let mut prev = None; let mut node = head; loop { match &mut node { Some(n) => { let next = n.next.take(); n.next = prev; prev = node; node = next; } None => break, } } prev } }
C 解法, 执行用时: 2ms, 内存消耗: 384KB, 提交时间: 2022-01-30
/** * struct ListNode { * int val; * struct ListNode *next; * }; * * C语言声明定义全局变量请加上static,防止重复定义 */ /** * * @param pHead ListNode类 * @return ListNode类 */ struct ListNode* ReverseList(struct ListNode* pHead ) { // write code here if(NULL == pHead) return NULL; if(NULL == pHead->next) return pHead; struct ListNode* l = pHead; struct ListNode* r = pHead->next; pHead->next = NULL; while(r) { struct ListNode* pTmp = r->next; r->next = l; l = r; r = pTmp; } return l; }
C 解法, 执行用时: 2ms, 内存消耗: 384KB, 提交时间: 2022-01-03
/** * struct ListNode { * int val; * struct ListNode *next; * }; * * C语言声明定义全局变量请加上static,防止重复定义 */ /** * * @param pHead ListNode类 * @return ListNode类 */ struct ListNode* ReverseList(struct ListNode* pHead ) { if (pHead == NULL) { return NULL; } struct ListNode* pre; struct ListNode* p; struct ListNode* next; struct ListNode* tail; pre = NULL; p = pHead; while (p->next != NULL) { next = p->next; p->next = pre; pre = p; p = next; //p = p->next; } p->next = pre; return p; // write code here }
C 解法, 执行用时: 2ms, 内存消耗: 384KB, 提交时间: 2021-11-22
/** * struct ListNode { * int val; * struct ListNode *next; * }; * * C语言声明定义全局变量请加上static,防止重复定义 */ /** * * @param pHead ListNode类 * @return ListNode类 */ struct ListNode* ReverseList(struct ListNode* pHead ) { // write code here if(pHead == NULL) return NULL; if(pHead->next == NULL) return pHead; struct ListNode *l = pHead; struct ListNode *r = pHead->next; pHead->next = NULL; while(r){ struct ListNode *pTmp = r->next; r->next = l; l = r; r = pTmp; } return l; }
下一题