❓剑指 Offer 24. 反转链表
难度:简单
定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。
示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
限制:
0 <= 节点个数 <= 5000
注意:本题与 206. 反转链表 相同。
💡思路:
法一:递归
可以将本问题分解成子问题:
1->(剩余部分的反转),而1 始终指向 2,即1->2- 所以第一层递归的结果以为: 
5->4->3->2<-1 - 每一层以此类推。
 
法二:迭代
- 头插法。
 
🍁代码:(C++、Java)
法一:递归
 C++
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
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;
    }
};
 
Java
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
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;
    }
}
 
法二:迭代
 C++
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* h0 = new ListNode(-1);
        ListNode* temp = head;
        while(head != nullptr){
            head = head->next;
            temp->next = h0->next;
            h0->next = temp;
            temp = head;
        }
        head = h0->next;
        delete(h0);
        return head;
    }
};
 
Java
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode h0 = new ListNode(-1);
        ListNode temp = head;
        while(head != null){
            head = head.next;
            temp.next = h0.next;
            h0.next = temp;
            temp = head;
        }
        head = h0.next;
        return head;
    }
}
 
🚀 运行结果:

🕔 复杂度分析:
- 时间复杂度: 
      
       
        
        
          O 
         
        
          ( 
         
        
          n 
         
        
          ) 
         
        
       
         O(n) 
        
       
     O(n),其中 
n为链表的长度。 - 空间复杂度: 
      
       
        
        
          O 
         
        
          ( 
         
        
          1 
         
        
          ) 
         
        
       
         O(1) 
        
       
     O(1),递归空间复杂度为 
      
       
        
        
          O 
         
        
          ( 
         
        
          n 
         
        
          ) 
         
        
       
         O(n) 
        
       
     O(n),主要取决于递归调用的栈空间,最多为 
n层。而迭代只需要常数级额外空间,即为 O ( 1 ) O(1) O(1)。 
题目来源:力扣。
放弃一件事很容易,每天能坚持一件事一定很酷,一起每日一题吧!
关注我LeetCode主页 / CSDN—力扣专栏,每日更新!


















![[Python] flask运行+wsgi切换生产环境+supervisor配置指南](https://img-blog.csdnimg.cn/0df4f0ff878c4a2b83ede2637f6cf659.png)