python”’
def trainningPlan(self, head):
“””
:type head: Optional[ListNode]
:rtype: Optional[ListNode]
“””
#递归反转
# 如果链表为空或只有一个节点,直接返回该节点
if head is None or head.next is None:
return head
# 递归
temp = self.trainningPlan(head.next)
head.next.next = head
head.next = None
return temp
时间空间复杂度为O(n)
def trainningPlan(self, head):
“””
:type head: Optional[ListNode]
:rtype: Optional[ListNode]
“””
#原地反转–一个while循环实现,
#通过迭代的方式逐步改变每个节点的next指针,直到整个链表被反转
pre = None
current = head
while current is not None:
next_node = current.next
current.next = pre
pre = current
current = next_node
return pre
时间复杂度O(n),空间复杂度O(1)
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def trainningPlan(self, head):
"""
:type head: Optional[ListNode]
:rtype: Optional[ListNode]
"""
cur, pre = head, None
while cur:
tmp = cur.next # 暂存后继节点 cur.next
cur.next = pre # 修改 next 引用指向
pre = cur # pre 暂存 cur
cur = tmp # cur 访问下一节点
return pre
”’python
class Solution(object):
def reverseList(self, head):
“””
:type head: ListNode
:rtype: ListNode
“””
if head is None or head.next is None:
return head
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def trainningPlan(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head is None or head.next is None:
return head
# 反转子链表
temp = self.trainningPlan(head.next)
head.next.next = head
head.next = None
return temp
class Solution:
def trainningPlan(self, head: Optional[ListNode]) -> Optional[ListNode]:
#双指针法
cur,pre=head,None
while cur:
tmp = cur.next
cur.next = pre
pre = cur
cur = tmp
return pre
时间复杂度:o(n)
空间复杂度:o(n)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
# 递归反转
class Solution:
def trainningPlan(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return head
# 1、定义一个函数反转链表
def reverse(cur, pre):
# 2、设置基线条件,定义递归出口
if cur is None:
return pre
# 3、设置递归条件,定义递归循环
tmp = cur.next
cur.next = pre
return reverse(tmp, cur)
return reverse(head, None)
# 原地反转
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head is None:
return None
pre = None
cur = head
while cur :
tmp = cur.next
cur.next = pre
pre = cur
cur = tmp
return pre
“`python
class Solution:
def trainningPlan(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return head
# 基础题,递归反转+原地反转,两种方法都要掌握
# 1、递归
dummy = ListNode(-1)
cur = self.reverse(head, dummy)
cur.next = None
return dummy.next
def reverse(self, cur, dummy):
if not cur.next:
dummy.next = cur
return cur
pre = self.reverse(cur.next, dummy)
pre.next = cur
return cur
原地反转 即普通迭代
class Solution:
def trainningPlan(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return head
pre = None
cur = head
while cur:
temp = cur.next
cur.next = pre
pre = cur
cur = temp
return pre
共以下 80 个回答
python”’
def trainningPlan(self, head):
“””
:type head: Optional[ListNode]
:rtype: Optional[ListNode]
“””
#递归反转
# 如果链表为空或只有一个节点,直接返回该节点
if head is None or head.next is None:
return head
# 递归
temp = self.trainningPlan(head.next)
head.next.next = head
head.next = None
return temp
时间空间复杂度为O(n)
def trainningPlan(self, head):
“””
:type head: Optional[ListNode]
:rtype: Optional[ListNode]
“””
#原地反转–一个while循环实现,
#通过迭代的方式逐步改变每个节点的next指针,直到整个链表被反转
pre = None
current = head
while current is not None:
next_node = current.next
current.next = pre
pre = current
current = next_node
return pre
时间复杂度O(n),空间复杂度O(1)
原地反转
首先判断特殊情况head是否为空,head下一个节点是否为空
因为要初始化两个,一个是cur,一个是pre
当cur不为空就进行反转,反转之前要保存一下cur下一个节点的信息,之后整体去移动
最后返回pre,也就是反转后的头节点
递归方法
首先判断特殊情况为空
然后对子链表进行反转,这里lecoode没有提供反转方法会报错
然后把头节点的下一个节点指向头节点,头节点本身指向null
就结束了
原地反转
首先判断特殊情况head是否为空,head下一个节点是否为空
因为要初始化两个,一个是cur,一个是pre
当cur不为空就进行反转,反转之前要保存一下cur下一个节点的信息,之后整体去移动
最后返回pre,也就是反转后的头节点
递归方法
首先判断特殊情况为空
然后对子链表进行反转,这里lecoode没有提供反转方法会报错
然后把头节点的下一个节点指向头节点,头节点本身指向null
就结束了
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode trainningPlan(ListNode* head) {
// 判断递归结束条件
if(head NULL || head->next NULL)
{
return head;
}
// 递归等式
//反转整个链表等价于反转trainingPlan(head.next)+原地反转第1个链表
ListNode* newListNode = trainningPlan(head->next);
};
//今天学了递归算法,递归要义:确定函数功能,确定结束条件,确定递归等式
第六次打卡
”’python
class Solution(object):
def reverseList(self, head):
“””
:type head: ListNode
:rtype: ListNode
“””
if head is None or head.next is None:
return head
“`c++
//原地反转,空间O(1),时间O(n)
class Solution {
public:
ListNode* trainningPlan(ListNode* head) {
ListNode* pre = head;
ListNode* cur = nullptr;
while(pre!=nullptr){
ListNode* tmp = pre;
pre = pre->next;
tmp->next = cur;
cur = tmp;
}
return cur;
};
//递归,时间O(n),空间O(n)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode next;
* };
*/
struct ListNode reverseList(struct ListNode* head) {
struct ListNode* pre = NULL;
struct ListNode* cur = head;
while(cur!=NULL){
struct ListNode* temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
return pre;
}打卡
时间:O(n)
空间:O(1)
//使用头插法进行反转
struct ListNode* trainningPlan(struct ListNode* head) {
typedef struct ListNode ListNode;
if(head NULL){
return NULL;
}
ListNode *p = head->next;
head->next = NULL;
ListNode *r =NULL;
while(p!=NULL){
r=p->next;
p->next = head;
head = p;
p = r;
}
}
方法一:双指针,时间复杂度O(n),空间复杂度O(1)
class Solution {
public ListNode trainningPlan(ListNode head) {
if(head null || head.next null) return head;
ListNode pre = null;
ListNode cur = head;
while(cur != null) {
ListNode curNext = cur.next;
cur.next = pre;
pre = cur;
cur = curNext;
}
return pre;
}
}
方法二:递归,时间复杂度O(n),空间复杂度O(1)
class Solution {
ListNode res = null;
public ListNode trainningPlan(ListNode head) {
ListNode pre = null;
recursion(head,pre);
return res;
}
public void recursion(ListNode cur,ListNode pre) {
if(cur null) {
res = pre;
return;
}
recursion(cur.next, cur);
cur.next = pre;
}
}
public ListNode trainningPlan(ListNode head)
{
if(headnull||head.nextnull)
return head;
ListNode pre=trainningPlan(head.next);
head.next.next=head;
head.next=null;
return pre;
}
利用递归方式,从后往前反转链表
时间复杂度O(n)
空间复杂度O(1)
//思路:双指针,不停修改cur和pre,但是每次都提前存下cur->next
//思路一:每次都让cur指向pre,同时为了向后走,提前记录next
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode trainningPlan(ListNode* head) {
if(head NULL || head->next NULL) return head;
ListNode *pre = NULL;
ListNode *cur = head;
ListNode *tmp;
};
时间复杂度O(n)
空间复杂度O(1)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode trainningPlan(ListNode head) {
if(head null) return head;
}
时间复杂度O(n),空间复杂度O(1)
public ListNode trainningPlan(ListNode head) {
ListNode cur = head, pre = null;
while(cur != null) {
ListNode tmp = cur.next; // 暂存后继节点 cur.next
cur.next = pre; // 修改 next 引用指向
pre = cur; // pre 暂存 cur
cur = tmp; // cur 访问下一节点
}
return pre;
}
思路:cur指向当前 把cur的前一个变成cur的后一个
时间 n 空间1
原地反转,空间复杂度O(1),时间复杂度O(n)
“`C++
class Solution {
public:
ListNode* trainningPlan(ListNode* head) {
ListNode* pre = nullptr;
ListNode* cur = head;
};
“`
#打卡
var trainningPlan = function(head) {
if (head null || head.next null) {
return head;
}
const newHead = trainningPlan(head.next);
head.next.next = head;
head.next = null;
return newHead;
};
时间复杂度 O(N)
空间复杂度O(1)
定义当前节点cur,前节点pre和临时节点tmp;设法让当前节点指向前节点;
1、临时节点指向cur的下一个节点
2、当前节点的下一个节点修改指向为前节点
3、前节点修改为当前节点
4、当前节点修改为临时节点
时间复杂度:O(N)
空间复杂度:O(1)
思路:
用三个指针,遍历链表,执行断链,重定向,将原链表逆序,输出
class Solution {
public:
ListNode* trainningPlan(ListNode* head) {
ListNode *cur = head, *pre =nullptr;
while(cur != nullptr){
ListNode *temp = cur->next;//暂存后继结点
cur->next = pre;
pre = cur;
cur = temp;
}
return pre;
}
};
Definition for singly-linked list.
class ListNode(object):
def init(self, val=0, next=None):
self.val = val
self.next = next
class Solution(object):
def trainningPlan(self, head):
“””
:type head: Optional[ListNode]
:rtype: Optional[ListNode]
“””
if head is None:
return None
常规
java
class Solution {
public ListNode trainningPlan(ListNode head) {
ListNode cur = head, pre = null;
while(cur != null){
ListNode tmp = cur.next;
cur.next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
}
提交
原地反转
class Solution {
public ListNode reverseList(ListNode head) {
//tmp用来下一节点,cur用来记录当前节点,pre用来记录上一届点
while (cur != null){
ListNode tmp = cur.next;
cur.next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
}
class Solution:
def trainningPlan(self, head: Optional[ListNode]) -> Optional[ListNode]:
#双指针法
cur,pre=head,None
while cur:
tmp = cur.next
cur.next = pre
pre = cur
cur = tmp
return pre
时间复杂度:o(n)
空间复杂度:o(n)
使用递归方法:
递推+回溯
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
if head:
return self.reverseBookList(head.next) + [head.val]
else:
return []
node为空的时候为递推的终止,回溯的起点
使用递归方法:
递推+回溯
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
if head:
return self.reverseBookList(head.next) + [head.val]
else:
return []
node为空的时候为递推的终止,回溯的起点
使用递归方法:
递推+回溯
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
if head:
return self.reverseBookList(head.next) + [head.val]
else:
return []
node为空的时候为递推的终止,回溯的起点
使用递归方法:
递推+回溯
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
if head:
return self.reverseBookList(head.next) + [head.val]
else:
return []
node为空的时候为递推的终止,回溯的起点
使用递归方法:
递推+回溯
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
if head:
return self.reverseBookList(head.next) + [head.val]
else:
return []
node为空的时候为递推的终止,回溯的起点
使用递归方法:
递推+回溯
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
if head:
return self.reverseBookList(head.next) + [head.val]
else:
return []
node为空的时候为递推的终止,回溯的起点
使用递归方法:
递推+回溯
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
if head:
return self.reverseBookList(head.next) + [head.val]
else:
return []
node为空的时候为递推的终止,回溯的起点
使用递归方法:
递推+回溯
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
if head:
return self.reverseBookList(head.next) + [head.val]
else:
return []
node为空的时候为递推的终止,回溯的起点
使用递归方法:
递推+回溯
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
if head:
return self.reverseBookList(head.next) + [head.val]
else:
return []
node为空的时候为递推的终止,回溯的起点
使用递归方法:
递推+回溯
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
if head:
return self.reverseBookList(head.next) + [head.val]
else:
return []
node为空的时候为递推的终止,回溯的起点
使用递归方法:
递推+回溯
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
if head:
return self.reverseBookList(head.next) + [head.val]
else:
return []
node为空的时候为递推的终止,回溯的起点
使用递归方法:
递推+回溯
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
if head:
return self.reverseBookList(head.next) + [head.val]
else:
return []
node为空的时候为递推的终止,回溯的起点
使用递归方法:
递推+回溯
class Solution:
def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
if head:
return self.reverseBookList(head.next) + [head.val]
else:
return []
node为空的时候为递推的终止,回溯的起点
递归思想 O(n)O(n)加原地反转O(n) O(1)
递归思想重在理解思路及跳出条件
原地反转理解头部在哪即可
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
// //递归
ListNode trainningPlan(ListNode* head) {
// if(headnullptr||head->nextnullptr)
// {
// return head;
// }
// ListNode *temp = trainningPlan(head->next);
// head->next->next = head;
// head->next=nullptr;
// return temp;
//原地反转
if(headnullptr||head->nextnullptr)
{
return head;
}
ListNode *pre = nullptr;
ListNode *cur = head;
while(cur!=nullptr)
{
ListNode *temp = cur->next;
cur->next = pre;
pre= cur;
cur = temp;
}
return pre;
}
};
时间复杂度O(N)
空间复杂度O(1)
复杂度On
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode trainningPlan(ListNode head) {
if(head null || head.next null ) return head;
ListNode pre = null;
ListNode node = head;
ListNode next = head.next;
while(node != null){
next = node.next;
node.next = pre;
pre = node;
node = next;
}
return pre;
}
class Solution {
public ListNode trainningPlan(ListNode head) {
//原地反转
if(head null || head.next null){
return head;
}
}
7行递归
时间复杂度o(n)
直接倒转
时间复杂度O(N),空间复杂度O(1)
class Solution {
// 原地反转
public ListNode reverseList(ListNode head) {
if(head null || head.next null){
return head;
}
}
使用双指针比较能理解,但是如果用递归暂时较难消化。
时间复杂度O(n)
空间复杂度O(1)
时间复杂度O(n),空间复杂度O(n)
class Solution { stk;
public:
ListNode* trainningPlan(ListNode* head) {
stack
if(headnullptr) return nullptr;
ListNode*p =head;
while(p!=nullptr)
{
stk.push(p->val);
p=p->next;
}
p=head;
while(p!=nullptr)
{
p->val=stk.top();
stk.pop();
p=p->next;
}
return head;
}
};
使用栈反转
时间复杂度为O(n),空间复杂度为O(n)
struct ListNode* trainningPlan(struct ListNode* head) {
if(head NULL)
{
return head;
}
struct ListNode *cur = head;
struct ListNode *prev = NULL;
struct ListNode *temp = NULL;
while(cur)
{
temp = cur->next;
cur->next = prev;
prev = cur;//更新prev到当前
cur = temp;
}
return prev;
}
思路:原地反转
时间复杂度:O(n);空间复杂度:O(1)
递归方式实现链表反转
时间复杂度O(N) 额外空间复杂度O(n)
递归方法
时间复杂度$O(N)$,空间复杂度$O(N)$(递归内置栈)
标准双指针方法
时间复杂度$O(N)$,空间复杂度$O(1)$
打卡
https://leetcode.cn/problems/fan-zhuan-lian-biao-lcof/solutions/2664482/liang-chong-fang-fa-fan-zhuan-lian-biao-s7xvw
// 递归
public ListNode reverseList(ListNode head) {
if(head null || head.next null){
return head;
}
// 反转子链表
ListNode temp = reverseList(head.next);
head.next.next = head;
head.next = null;
class Solution {
public ListNode trainningPlan(ListNode head) {
ListNode pre = null, cur = head;
while (cur != null) {
ListNode tmp = cur.next;
cur.next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
}
时间复杂度O(n)
Definition for singly-linked list.
class ListNode:
def init(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def trainningPlan(self, head: Optional[ListNode]) -> Optional[ListNode]:
时间o(n) 空间o(1)
//双指针
class Solution {
public:
ListNode* trainningPlan(ListNode* head) {
ListNode* pre = NULL;
ListNode* cur = head;
ListNode* tmp;
while(cur){
tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
};
//dfs
class Solution {
public:
ListNode* reverse(ListNode* pre,ListNode* cur){
if(cur NULL) return pre;
ListNode* temp = cur->next;
cur->next = pre;
// 可以和双指针法的代码进行对比,如下递归的写法,其实就是做了这两步
// pre = cur;
// cur = temp;
return reverse(cur,temp);
}
ListNode* reverseList(ListNode* head) {
// 和双指针法初始化是一样的逻辑
// ListNode* cur = head;
// ListNode* pre = NULL;
return reverse(NULL, head);
}
};
递归反转:
时间复杂度O(n)
空间复杂度O(n):压栈的额外空间n
原地反转:
时间复杂度O(n)
空间复杂度O(1)
//迭代 时间On 空间O1
class Solution {
public ListNode trainningPlan(ListNode head) {
if(head null)
return head;
ListNode fastPtr = head.next;
ListNode slowPtr = head;
ListNode temp = fastPtr;
slowPtr.next = null;
while(fastPtr != null){
temp = fastPtr.next;
fastPtr.next = slowPtr;
slowPtr = fastPtr;
fastPtr = temp;
}
return slowPtr;
}
//递归时间On 空间On
class Solution {
private ListNode recur(ListNode pre,ListNode cur){
if(cur null)
return pre;
ListNode head = recur(cur,cur.next);
cur.next = pre;
return head;
}
递归:时间复杂度o(n) 空间复杂度o(n)
迭代:时间复杂度o(n) 空间复杂度o(n)
“`CPP
递归
class Solution {
public ListNode trainningPlan(ListNode head) {
return recur(head, null) ;
}
}
原地反转:
class Solution {
public ListNode trainningPlan(ListNode head) {
ListNode now = head;
ListNode pre = null;
while(now != null){
ListNode tmp = now.next;
now.next = pre;
pre = now;
now = tmp;
}
return pre;
}
}时间复杂度都为O(N)空间复杂度为O(N)和O(1)
class Solution {
public ListNode trainningPlan(ListNode head) {
ListNode res = recur(null,head);
return res;
}
public ListNode recur(ListNode pre, ListNode cur){
if(cur null)return pre; // 递归出口
ListNode temp = cur.next;
cur.next = pre;
return recur(cur,temp);
}
}
day2第一题
反转链表java
递归
原地反转
时间:O(n)
空间:O(n)
递归
“`python
class Solution:
def trainningPlan(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return head
# 基础题,递归反转+原地反转,两种方法都要掌握
# 1、递归
dummy = ListNode(-1)
cur = self.reverse(head, dummy)
cur.next = None
return dummy.next
原地反转 即普通迭代
class Solution:
def trainningPlan(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return head
pre = None
cur = head
while cur:
temp = cur.next
cur.next = pre
pre = cur
cur = temp
return pre
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode trainningPlan(ListNode* head) {
if(!head || !head->next) return head;
ListNode* pre = nullptr;
ListNode* cur = head;
while(cur){
ListNode* tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
};
class Solution {
public:
ListNode* trainningPlan(ListNode* head) {
if(!head || !head->next) return head;
ListNode* tmp = trainningPlan(head->next);
//到末尾停止循环,此时tmp = head = 末尾
head->next->next = head;
head->next = nullptr;
return tmp;
}
};
思路:双指针加临时变量,复杂度:O(n)
代码:
“`java
public ListNode trainningPlan(ListNode head) {
ListNode temp=null;
ListNode pre=null;
ListNode cur=head;
while (null != cur) {
temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
时间复杂度:O(n)
空间复杂度:O(1)
为什么我也是
“`python 但是敲不出来这个代码块啊,求教
class Solution {
public:
ListNode* trainningPlan(ListNode* head) {
ListNode* cur, *pre, *tmp;
cur = nullptr;
pre = head;
tmp = head;
while (pre != nullptr){
tmp = pre->next;
pre->next = cur;
cur = pre;
pre = tmp;
}
return cur;
}
};时间复杂度N 空间复杂度1
“`java
class Solution {
public ListNode trainningPlan(ListNode head) {
ListNode head1 = new ListNode();
while(head != null){
ListNode next = head.next;
head.next = head1.next;
head1.next = head;
head = next;
}
return head1.next;
}
}
“` 反转链表常用就地算法就是头插法
“`java
class Solution {
public ListNode trainningPlan(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode newHead = trainningPlan(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
}
思路:递归寻找节点,从链表的尾节点开始修改引用指向,让head.next.next = head,这样就可以修改引用指向,同时还需要修改链表原来的头节点的next值,不然可能会导致有环存在,所以需要head.next=null
思路:创建一个虚拟头节点prehead, 依次遍历head链表,将head链表进行反转。
时间复杂度:O(n)
空间复杂度:O(1)
“`c++
class Solution {
public:
ListNode* trainningPlan(ListNode* head) {
if (head == nullptr || head->next == nullptr) return head;
ListNode *prehead = nullptr;
ListNode *cur = nullptr;
while (head) {
cur = head->next;
head->next = prehead;
prehead = head;
head = cur;
}
return prehead;
}
};
“`