【链表专题】剑指 Offer 24. 反转链表

本问题对应课程链接:剑指offer刷题第六期

要求:

1、请打卡你的解题代码,如果能简单说一下思路则简单说一下思路。
2、给出你代码所用的时间复杂度和空间复杂度

共以下 80 个回答

  • mpweixin用户 普通 2024年4月1日 上午11:39

    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)

    0 赞同 0 条回复
  • 晚睡晚起起不来 永久会员 2024年3月30日 上午11:22

    原地反转
    首先判断特殊情况head是否为空,head下一个节点是否为空
    因为要初始化两个,一个是cur,一个是pre
    当cur不为空就进行反转,反转之前要保存一下cur下一个节点的信息,之后整体去移动
    最后返回pre,也就是反转后的头节点

    java
    class Solution {
        public ListNode trainningPlan(ListNode head) {
            //原地反转
    //判断特殊情况是否为空
            if(head ==null || head.next ==null) return head;
    //初始化节点,cur 和pre
            ListNode cur = head, pre = null;
    //当cur存在的时候,进行反转
            while(cur != null){
    //temp在反转之前是保存当前节点的下一个节点信息的
                ListNode temp = cur.next;
    //cur.next指向pre ,单节点是反转的,while循环后,整个指向就相反了
                cur.next = pre;
    //整体移动
                pre = cur;
                cur = temp;
            }
    //最后返回pre,其中pre是反转后的头节点,是原链表的尾部
            return pre;
    
        }
    }
    
    
    

    递归方法
    首先判断特殊情况为空
    然后对子链表进行反转,这里lecoode没有提供反转方法会报错
    然后把头节点的下一个节点指向头节点,头节点本身指向null
    就结束了

    java
      // 递归
          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;
    
            return temp;
        }
    
    0 赞同 0 条回复
  • 晚睡晚起起不来 永久会员 2024年3月30日 上午11:22

    原地反转
    首先判断特殊情况head是否为空,head下一个节点是否为空
    因为要初始化两个,一个是cur,一个是pre
    当cur不为空就进行反转,反转之前要保存一下cur下一个节点的信息,之后整体去移动
    最后返回pre,也就是反转后的头节点

    java
    class Solution {
        public ListNode trainningPlan(ListNode head) {
            //原地反转
    //判断特殊情况是否为空
            if(head ==null || head.next ==null) return head;
    //初始化节点,cur 和pre
            ListNode cur = head, pre = null;
    //当cur存在的时候,进行反转
            while(cur != null){
    //temp在反转之前是保存当前节点的下一个节点信息的
                ListNode temp = cur.next;
    //cur.next指向pre ,单节点是反转的,while循环后,整个指向就相反了
                cur.next = pre;
    //整体移动
                pre = cur;
                cur = temp;
            }
    //最后返回pre,其中pre是反转后的头节点,是原链表的尾部
            return pre;
    
        }
    }
    
    
    

    递归方法
    首先判断特殊情况为空
    然后对子链表进行反转,这里lecoode没有提供反转方法会报错
    然后把头节点的下一个节点指向头节点,头节点本身指向null
    就结束了

    java
      // 递归
          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;
    
            return temp;
        }
    
    0 赞同 0 条回复
  • 山月 普通 2024年3月14日 下午8:44

    /**
    * 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);

        head->next->next = head;
        head->next =NULL;
        return newListNode;
    
    
    }
    

    };
    //今天学了递归算法,递归要义:确定函数功能,确定结束条件,确定递归等式

    0 赞同 0 条回复
  • RunAsOne 普通 2024年3月11日 下午4:29

    第六次打卡

    # 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
    
    
    
    0 赞同 0 条回复
  • 雨落星空 普通 2024年3月10日 上午9:43

    ”’python
    class Solution(object):
    def reverseList(self, head):
    “””
    :type head: ListNode
    :rtype: ListNode
    “””
    if head is None or head.next is None:
    return head

        # 反转子链表
        temp = self.reverseList(head.next)
        head.next.next = head
        head.next = None
    
        return temp
    
    0 赞同 0 条回复
  • Yeah~ 普通 2024年3月6日 下午2:19

    “`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)

    0 赞同 0 条回复
  • e=mc² 普通 2024年3月6日 上午12:22
    // 原地
    class Solution {
        public ListNode trainningPlan(ListNode head) {
            ListNode pre =null;
            // 不影响原链表
            ListNode cur = head;
            while (cur != null) {
                // 保存未遍历到的部分
                ListNode temp = cur.next;
                // 取下一位,使其next拼接上已经倒转的部分
                cur.next = pre;
                // 保存已经遍历的部分
                pre = cur;
                //继续遍历
                cur = temp;
            }
            return pre;
        }
    }
    时间复杂度:O(n)
    时间复杂度:O(1)
    
    //递归
    class Solution {
        public ListNode trainningPlan(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
            // 没有复制新的链表
            ListNode newHead = trainningPlan(head.next);
            // 改变的是next指针的指向
            head.next.next = head;
            head.next = null;
            return newHead;
        }
    }
    时间复杂度:O(n)
    空间复杂度:O(n)
    
    0 赞同 0 条回复
  • 湘江小水鬼 普通 2024年3月5日 下午10:06

    /**
    * 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;
    }打卡

    0 赞同 0 条回复
  • YOLO 普通 2024年3月5日 下午7:34
     //定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。
    class Solution {
        public ListNode trainningPlan(ListNode head) {
            if(head==null||head.next==null)
                return head;
    
            ListNode cur = head;
            ListNode pre = null;
            while(cur != null){
                ListNode temp = cur.next;
                cur.next = pre;
                pre = cur;
                cur = temp;
            }
            return pre;
        }
    }
    
    0 赞同 0 条回复
  • C C 普通 2024年3月5日 下午7:15
    # 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
    
    0 赞同 0 条回复
  • 神经蛙 普通 2024年3月5日 上午10:38
    class Solution {
        public ListNode trainningPlan(ListNode head) {
            if(head == null || head.next == null) return head;
            ListNode pre = null, cur = head;
    
            while(cur != null){
                ListNode n = cur.next;
                // 交换指向
                cur.next = pre;
                // 移动
                pre = cur;
                cur = n;
            }
    
            return pre;
        }
    }
    
    0 赞同 0 条回复
  • Thaddeus 永久会员 2024年3月5日 上午8:02

    时间:O(n)
    空间:O(1)

    class Solution {
    public:
        ListNode* trainningPlan(ListNode* head) {
            if(head == nullptr || head->next == nullptr) return head;
    
            ListNode* cur = head;
            ListNode* pre = nullptr;
            while(cur) {
                ListNode* nextNode = cur->next; // 保留cur的下一个点
                cur->next = pre; // 然后每次只改变两个节点之间的那个指针方向
                pre = cur;
                cur = nextNode; // cur移动到下一个点
            }
            return pre;
        }
    };
    时间:O(n)
    空间:O(n)
    class Solution {
    public:
        ListNode* trainningPlan(ListNode* head) {
            if(!head || !head->next) return head;
            ListNode* newHead = trainningPlan(head->next);
            head->next->next = head;
            head->next = nullptr;
            return newHead;
        }
    };
    
    0 赞同 0 条回复
  • 时生 普通 2024年3月5日 上午12:16

    //使用头插法进行反转
    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;
    }

    return head;
    

    }

    0 赞同 0 条回复
  • 繁星 普通 2024年3月4日 下午11:33

    方法一:双指针,时间复杂度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;
    }
    }

    0 赞同 0 条回复
  • 莫~慢~待 普通 2024年3月4日 下午11:16

    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)

    0 赞同 0 条回复
  • 30tcq 普通 2024年3月4日 下午8:17

    //思路:双指针,不停修改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;

        while(cur != NULL){
            tmp = cur->next;
            cur->next = pre;                                                                                           
            pre = cur;
            cur = tmp; 
        }
        return pre;
    }
    

    };
    时间复杂度O(n)
    空间复杂度O(1)

    0 赞同 0 条回复
  • =.= 普通 2024年3月4日 下午7:59

    /**
    * 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;

        ListNode pre = null;
        ListNode cur = head;
    
        while(cur != null){
            ListNode node = cur.next;
            cur.next = pre;
            pre = cur;
            cur = node;
        }
    
        return pre;
    }
    

    }

    时间复杂度O(n),空间复杂度O(1)

    0 赞同 0 条回复
  • mpweixin用户 普通 2024年3月4日 下午5:33

    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

    0 赞同 0 条回复
  •  南木 普通 2024年3月4日 下午5:15

    原地反转,空间复杂度O(1),时间复杂度O(n)

    /**
     * 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) {
            ListNode cur = head,pre = null;
            ListNode temp = new ListNode();
            while(cur != null){
                temp = cur.next;
                cur.next = pre;
                pre = cur;
                cur = temp;
            }
            return pre;
        }
    }
    
    0 赞同 0 条回复
  • 小耿 普通 2024年3月4日 下午4:11

    “`C++
    class Solution {
    public:
    ListNode* trainningPlan(ListNode* head) {
    ListNode* pre = nullptr;
    ListNode* cur = head;

        if (!head) return head; //nullptr
        ListNode* tmp = nullptr;
    
        while(cur) {
            tmp = cur->next;
            cur->next = pre;
            pre = cur;  // 这个不能忘记移动
            if (tmp == nullptr) break;  // 这个判断
            cur = tmp;
        }
        return cur;
    }
    

    };

    “`

    0 赞同 0 条回复
  • 阿野 普通 2024年3月4日 下午4:11

    #打卡
    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;
    };

    0 赞同 0 条回复
  • 南梁陈庆之 普通 2024年3月4日 下午3:50
    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;
    
        }
    }
    

    时间复杂度 O(N)
    空间复杂度O(1)

    定义当前节点cur,前节点pre和临时节点tmp;设法让当前节点指向前节点;
    1、临时节点指向cur的下一个节点
    2、当前节点的下一个节点修改指向为前节点
    3、前节点修改为当前节点
    4、当前节点修改为临时节点

    0 赞同 0 条回复
  • kfaebot 普通 2024年3月4日 下午3:41
    /**
     * 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) {
            ListNode *tmp=nullptr;
            ListNode *slow=nullptr;
            ListNode *fast=head;
    
            //如果链表为空或者链表只有一个元素,直接返回
            if(head==nullptr||head->next==nullptr){
                return head;
            }
            while(fast!=nullptr){
                tmp=fast->next;
                fast->next=slow;
                slow=fast;
                fast=tmp;
            }
            return slow;
        }
    };
    \\双指针原地反转
    \\时间复杂度O(N),空间复杂度O(1)
    
    0 赞同 0 条回复
  • Jankin 普通 2024年3月4日 下午1:30
    /**
     * 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 reverseList(ListNode head) {
            if (head == null || head.next == null)
                return head;
    
            // 递归
            // ListNode temp = reverseList(head.next);
            // head.next.next = head;
            // head.next = null;
            // return temp;
    
            // 迭代
            ListNode pre = null, cur = head;
            while (cur != null) {
                ListNode temp = cur.next;
                cur.next = pre;
                pre = cur;
                cur = temp;
            }
            return pre;
        }
    }
    

    时间复杂度:O(N)
    空间复杂度:O(1)

    0 赞同 0 条回复
  • 一个达不刘 普通 2024年3月4日 上午10:35

    思路:
    用三个指针,遍历链表,执行断链,重定向,将原链表逆序,输出

    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;
    }
    };

    0 赞同 0 条回复
  • Eugene 普通 2024年3月4日 上午8:37

    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

        node = ListNode(head.val)
        while head.next is not None:
            node = ListNode(head.next.val, node)
            head = head.next
        return node
    

    常规

    0 赞同 0 条回复
  • mpweixin用户 普通 2024年3月3日 下午10:03

    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;
    }
    }
    提交

    0 赞同 0 条回复
  • 东 普通 2024年3月3日 下午9:57

    原地反转
    class Solution {
    public ListNode reverseList(ListNode head) {

        ListNode pre = null;
        ListNode cur = head;
    

    //tmp用来下一节点,cur用来记录当前节点,pre用来记录上一届点
    while (cur != null){
    ListNode tmp = cur.next;
    cur.next = pre;
    pre = cur;
    cur = tmp;
    }
    return pre;
    }
    }

    0 赞同 0 条回复
  • 晚风、 普通 2024年3月3日 下午9:39

    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)

    0 赞同 0 条回复
  • 晚风、 普通 2024年3月3日 下午9:39

    使用递归方法:
    递推+回溯
    class Solution:
    def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
    if head:
    return self.reverseBookList(head.next) + [head.val]
    else:
    return []
    node为空的时候为递推的终止,回溯的起点

    0 赞同 0 条回复
  • 晚风、 普通 2024年3月3日 下午9:39

    使用递归方法:
    递推+回溯
    class Solution:
    def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
    if head:
    return self.reverseBookList(head.next) + [head.val]
    else:
    return []
    node为空的时候为递推的终止,回溯的起点

    0 赞同 0 条回复
  • 晚风、 普通 2024年3月3日 下午9:39

    使用递归方法:
    递推+回溯
    class Solution:
    def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
    if head:
    return self.reverseBookList(head.next) + [head.val]
    else:
    return []
    node为空的时候为递推的终止,回溯的起点

    0 赞同 0 条回复
  • 晚风、 普通 2024年3月3日 下午9:39

    使用递归方法:
    递推+回溯
    class Solution:
    def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
    if head:
    return self.reverseBookList(head.next) + [head.val]
    else:
    return []
    node为空的时候为递推的终止,回溯的起点

    0 赞同 0 条回复
  • 晚风、 普通 2024年3月3日 下午9:39

    使用递归方法:
    递推+回溯
    class Solution:
    def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
    if head:
    return self.reverseBookList(head.next) + [head.val]
    else:
    return []
    node为空的时候为递推的终止,回溯的起点

    0 赞同 0 条回复
  • 晚风、 普通 2024年3月3日 下午9:39

    使用递归方法:
    递推+回溯
    class Solution:
    def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
    if head:
    return self.reverseBookList(head.next) + [head.val]
    else:
    return []
    node为空的时候为递推的终止,回溯的起点

    0 赞同 0 条回复
  • 晚风、 普通 2024年3月3日 下午9:39

    使用递归方法:
    递推+回溯
    class Solution:
    def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
    if head:
    return self.reverseBookList(head.next) + [head.val]
    else:
    return []
    node为空的时候为递推的终止,回溯的起点

    0 赞同 0 条回复
  • 晚风、 普通 2024年3月3日 下午9:39

    使用递归方法:
    递推+回溯
    class Solution:
    def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
    if head:
    return self.reverseBookList(head.next) + [head.val]
    else:
    return []
    node为空的时候为递推的终止,回溯的起点

    0 赞同 0 条回复
  • 晚风、 普通 2024年3月3日 下午9:39

    使用递归方法:
    递推+回溯
    class Solution:
    def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
    if head:
    return self.reverseBookList(head.next) + [head.val]
    else:
    return []
    node为空的时候为递推的终止,回溯的起点

    0 赞同 0 条回复
  • 晚风、 普通 2024年3月3日 下午9:39

    使用递归方法:
    递推+回溯
    class Solution:
    def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
    if head:
    return self.reverseBookList(head.next) + [head.val]
    else:
    return []
    node为空的时候为递推的终止,回溯的起点

    0 赞同 0 条回复
  • 晚风、 普通 2024年3月3日 下午9:39

    使用递归方法:
    递推+回溯
    class Solution:
    def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
    if head:
    return self.reverseBookList(head.next) + [head.val]
    else:
    return []
    node为空的时候为递推的终止,回溯的起点

    0 赞同 0 条回复
  • 晚风、 普通 2024年3月3日 下午9:39

    使用递归方法:
    递推+回溯
    class Solution:
    def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
    if head:
    return self.reverseBookList(head.next) + [head.val]
    else:
    return []
    node为空的时候为递推的终止,回溯的起点

    0 赞同 0 条回复
  • 晚风、 普通 2024年3月3日 下午9:39

    使用递归方法:
    递推+回溯
    class Solution:
    def reverseBookList(self, head: Optional[ListNode]) -> List[int]:
    if head:
    return self.reverseBookList(head.next) + [head.val]
    else:
    return []
    node为空的时候为递推的终止,回溯的起点

    0 赞同 0 条回复
  • 一颗红豆 普通 2024年3月3日 下午8:22

    递归思想 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;
    }
    };

    0 赞同 0 条回复
  • Free 普通 2024年3月3日 下午8:15
    /**
     * 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 == nullptr || head->next == nullptr)
                return head;
            ListNode* newhead = new ListNode();
            ListNode* cur = head;
            while(cur)
            {
                ListNode* next = cur->next;
                cur->next = newhead->next;
                newhead->next = cur;
                cur = next;
            }
            return newhead->next;
        }
    };
    

    时间复杂度O(N)
    空间复杂度O(1)

    0 赞同 0 条回复
  • Monou 普通 2024年3月3日 下午7:54

    复杂度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;

    }
    

    }

    0 赞同 0 条回复
  • 墨丘俐 普通 2024年3月3日 下午7:15

    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 temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
    
    
    
        }
        return pre;
    
    }
    

    }

    7行递归

    0 赞同 0 条回复
  • 白雪公主倒拔垂杨柳 永久会员 2024年3月3日 下午6:40
    class Solution {
        public ListNode trainningPlan(ListNode head) {
            if(head==null) return null;
            //只有一个结点
            if(head.next==null){
                return head;
            }
    
            int len=1;
            ListNode p=head;
            ListNode r=head.next;
            ListNode q;
            //链表倒转
            p.next=null;
            while(r!=null){
                q=r.next;
                r.next=p;
                p=r;
                r=q;
                len++;
            }
    
            return p;
    
        }
    }
    

    时间复杂度o(n)

    0 赞同 0 条回复
  • 一期一会 普通 2024年3月3日 下午4:44

    直接倒转
    时间复杂度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) {
            ListNode *p = head, *q = nullptr;
            while(p != nullptr)
            {
                ListNode * tmp = p->next;
                p->next = q;
                q = p;
                p = tmp;
            }
            return q;
        }
    };
    
    0 赞同 0 条回复
  • 😴 普通 2024年3月3日 下午4:41

    class Solution {
    // 原地反转
    public ListNode reverseList(ListNode head) {
    if(head null || head.next null){
    return head;
    }

        ListNode cur = head, pre = null;
    
        while(cur != null){
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
    
        return pre;
    
    
    }
    

    }
    使用双指针比较能理解,但是如果用递归暂时较难消化。
    时间复杂度O(n)
    空间复杂度O(1)

    0 赞同 0 条回复
  • lucky耶耶耶*👻*~ 普通 2024年3月3日 下午3:45
    public class Solution {
        /**
         * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
         *
         *
         * @param head ListNode类
         * @return ListNode类
         */
        public ListNode ReverseList (ListNode head) {
            // write code here
            //跳出递归的情况
            if (head == null || head.next == null) {
                return head;
            }
            //递归前
            ListNode next = head.next;
            ListNode   reverse =  ReverseList(next);
            next.next = head;
            head.next = null;
            return reverse;
        }
    }
    
    0 赞同 0 条回复
  • Laboratory 普通 2024年3月3日 下午3:15
    /**
     * 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 newHead = trainningPlan(head.next);
            head.next.next = head;
            head.next = null;
            return newHead;
        }
    
    }
    

    时间复杂度O(n),空间复杂度O(n)

    0 赞同 0 条回复
  •     普通 2024年3月3日 下午2:02

    class Solution {
    public:
    ListNode* trainningPlan(ListNode* head) {
    stack stk;
    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)

    0 赞同 0 条回复
  • 神经蛙 普通 2024年3月3日 下午12:02

    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)

    0 赞同 0 条回复
  • 持之以恒:-) 普通 2024年3月3日 上午11:35

    递归方式实现链表反转

    时间复杂度O(N) 额外空间复杂度O(n)

    /**
     * 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) {
            // 1. 创建虚拟头节点
            ListNode virtualHead = new ListNode();
            reverseLinkedList(head, virtualHead);
            // 返回数据
            return virtualHead.next;
        }
    
        private ListNode reverseLinkedList(ListNode head, ListNode virtualHead){
            // 给上一层栈提供数据接口
            if(head == null)
                return virtualHead;
            virtualHead = reverseLinkedList(head.next, virtualHead);
            virtualHead.next = head;
            // 打破循环引用,打散每一个节点,因为每个栈帧都存放了一个节点
            head.next = null;
    
            return virtualHead.next;
        }
    }
    
    0 赞同 0 条回复
  • 方铎码农 普通 2024年3月3日 上午11:31

    递归方法

    时间复杂度$O(N)$,空间复杂度$O(N)$(递归内置栈)

    //递归方法
    class Solution {
        public ListNode trainningPlan(ListNode head) {
            return recur(head,null);
        }
    
        private ListNode recur (ListNode cur, ListNode pre){
            if (cur == null) return pre;
            ListNode res = recur(cur.next,cur);
            cur.next = pre;
            return res;
        }
    }
    

    标准双指针方法

    时间复杂度$O(N)$,空间复杂度$O(1)$

    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;
        }
    }
    
    0 赞同 0 条回复
  • ChenZhangHangTime 永久会员 2024年3月3日 上午10:59

    // 递归
    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;

        return temp;
    }
    
    0 赞同 0 条回复
  • Jimmy 普通 2024年3月3日 上午10:07
    1. 递归法,时间复杂度O(n),空间复杂度O(n)
    /**
     * 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* reverseList(ListNode* head) {
            if(head == nullptr || head->next == nullptr){
                return head;
            }
    
            ListNode *temp = reverseList(head->next);
            head->next->next = head;
            head->next = nullptr;
            return temp;
        }
    };
    
    1. 原地反转,时间复杂度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* reverseList(ListNode* head) {
            if(head == nullptr || head->next == nullptr){
                return head;
            }
    
            ListNode *pre = nullptr,*cur=head;
            while(cur != nullptr){
                ListNode *temp = cur->next;
                cur->next = pre;
                pre = cur;
                cur = temp;
            }
            return pre;
        }
    };
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2024年3月3日 上午9:12

    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)

    0 赞同 0 条回复
  • Yox 普通 2024年3月3日 上午6:01

    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]:

        cur,pre = head, None
        while cur:
            tmp = cur.next
            cur.next = pre
            pre = cur
            cur = tmp
        return pre
    

    时间o(n) 空间o(1)

    0 赞同 0 条回复
  • HU 普通 2024年3月3日 上午4:32
    /**
     * Definition for singly-linked list.
     * function ListNode(val, next) {
     *     this.val = (val===undefined ? 0 : val)
     *     this.next = (next===undefined ? null : next)
     * }
     */
    /**
     * @param {ListNode} head
     * @return {ListNode}
     */
    var trainningPlan = function(head) {
        // 1. 确定该函数的作用:返回反转链表的头节点
    
        // 2. 确定递归结束条件
        if(head == null || head.next == null) return head;
    
        // 3. 确定递归关系式
        let newHead = trainningPlan(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    };
    
    0 赞同 0 条回复
  • !。! 普通 2024年3月3日 上午12:04

    //双指针
    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);
    }

    };

    0 赞同 0 条回复
  • 叫不醒装睡的人 普通 2024年3月2日 下午11:26
    class Solution {
        // 反转链表考察原地反转和递归反转
        // 考虑临界情况,链表为空或者只有一个节点
        // 递归时要考虑到将当前节点的后继指向前驱,将原来的后继节点取下来
        // 原地排序需要借助两个指针,前驱指针和当前指针
        // 用一个临时节点存放当前节点的后继节点,将之前的后继指向前驱
        // 前驱后移,当前节点也后移
        public ListNode trainningPlan(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
            // 递归
            ListNode cur = trainningPlan(head.next);
            head.next.next = head;
            head.next = null;
            return cur;
    
            // 原地反转
            ListNode pre = null, cur = head;
            while (cur != null) {
                ListNode temp = cur.next;
                cur.next = pre;
                pre = cur;
                cur = temp;
            }
            return pre;
        }
        // 两种方法的时间复杂度都是 O(n)
        // 递归方法的空间复杂度为 O(n),原地排序方法的空间复杂度为 O(1)
    }
    
    0 赞同 0 条回复
  • Deckkker 普通 2024年3月2日 下午11:01
    # 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
    

    递归反转:
    时间复杂度O(n)
    空间复杂度O(n):压栈的额外空间n

    原地反转:
    时间复杂度O(n)
    空间复杂度O(1)

    0 赞同 0 条回复
  • 云无深雁影 普通 2024年3月2日 下午10:54

    //迭代 时间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;

    }
    public ListNode trainningPlan(ListNode head) {
        return recur(null,head);
    
    
    
    }
    

    }

    0 赞同 0 条回复
  • 机电皮怪 永久会员 2024年3月2日 下午10:50

    递归:时间复杂度o(n) 空间复杂度o(n)
    迭代:时间复杂度o(n) 空间复杂度o(n)

    “`CPP

    class Solution {
    public:
        //迭代
        ListNode* trainningPlan(ListNode* head) {
            if ( head == nullptr || head->next == nullptr )
            {
                return head;
            }
    
            ListNode* cur = head;
            ListNode* pre = nullptr;
            while(cur)
            {
                ListNode* temp = cur->next;
                cur->next = pre;
                pre = cur;
                cur = temp;
            }
            return pre;
        }
    
        //递归
        /*
        ListNode* trainningPlan(ListNode* head) {
            if ( head == nullptr )
            {
                return nullptr;
            }
    
            return newnext(head);
        }
    
        ListNode* newnext(ListNode* head)
        {
            //递归结束条件
            if( head == nullptr || head->next == nullptr )
            {
                return head;
            }
    
            ListNode* newhead = newnext(head->next);
            head->next->next = head;
            head->next = nullptr;
    
            return newhead;
        }
        */
    };
    
    0 赞同 0 条回复
  • Let it be 永久会员 2024年3月2日 下午10:47

    递归
    class Solution {
    public ListNode trainningPlan(ListNode head) {
    return recur(head, null) ;
    }

    public ListNode  recur(ListNode now, ListNode pre){
        if(now == null){
            return pre;
        }
        ListNode res = recur(now.next,now);
        now.next = pre;
        return res;
    }
    

    }
    原地反转:
    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)

    0 赞同 0 条回复
  • NULL 普通 2024年3月2日 下午10:28

    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);
    }
    }

    0 赞同 0 条回复
  • MARS 普通 2024年3月2日 下午9:39

    day2第一题

    function reverseBookList(head: ListNode | null): number[] {
      let ans: number[] = []
      reverse(head)
      function reverse(head: ListNode | null){
          if(!head){
              return
          }
          reverse(head.next)
          ans.push(head.val)
      }
      return ans
    };
    
    0 赞同 0 条回复
  • helloMoon 普通 2024年3月2日 下午9:34

    反转链表java

    递归

    class Solution {
        public ListNode trainningPlan(ListNode head) {
            if(head == null || head.next == null){
                return head;
            }
    
            ListNode tmp = trainningPlan(head.next);
            head.next.next = head;
            head.next = null;
    
            return tmp;
        }
    }
    

    原地反转

    class Solution {
        public ListNode trainningPlan(ListNode head) {
            if(head == null || head.next == null){
                return head;
            }
    
            ListNode cur = head, pre = null;
    
            while(cur != null){
                ListNode tmp = cur.next;
                cur.next = pre;
                pre = cur;
                cur = tmp;
            }
    
            return pre;
        }
    }
    
    0 赞同 0 条回复
  • 言之命至 普通 2024年3月2日 下午9:27
    class Solution {
        public ListNode trainningPlan(ListNode head) {
            if(head == null || head.next == null){
                return head;
            }
    
            ListNode temp = trainningPlan(head.next);
            head.next.next = head;
            head.next = null;
    
            return temp;
        }
    }
    

    时间:O(n)
    空间:O(n)

    0 赞同 0 条回复
  • Fancy 普通 2024年3月2日 下午9:21
    递归

    “`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

    0 赞同 0 条回复
  • Raphtalia. 普通 2024年3月2日 下午8:46

    /**
    * 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;
    }
    };

    0 赞同 0 条回复
  • 零 普通 2024年3月2日 下午8:19

    思路:双指针加临时变量,复杂度: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;
    }

    0 赞同 0 条回复
  • heathhou 普通 2024年3月2日 下午8:14
    class Solution {
    public:
        ListNode* trainningPlan(ListNode* head) {
            if(head == nullptr || head->next == nullptr ){
                return head;
            }
    
            ListNode * temp =  trainningPlan(head->next);
            head->next->next = head;
            head->next = nullptr;
    
            return temp;
        }
    };
    

    时间复杂度:O(n)
    空间复杂度:O(1)

    0 赞同 1 条回复
  • 进取 普通 2024年3月2日 下午8:13

    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

    0 赞同 0 条回复
  • 是hua不是pua 普通 2024年3月2日 下午7:22

    “`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;
    }
    }
    “` 反转链表常用就地算法就是头插法

    0 赞同 0 条回复
  • 王位詮釋 普通 2024年3月2日 下午6:41

    “`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

    0 赞同 0 条回复
  • 紫芝 普通 2024年3月2日 下午6:00

    思路:创建一个虚拟头节点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;
    }
    };
    “`

    0 赞同 0 条回复

# 回答此问题

后才能回答