zoukankan      html  css  js  c++  java
  • LeetCode 206. Reverse Linked List

    Reverse a singly linked list.

    click to show more hints.

    Hint:

    A linked list can be reversed either iteratively or recursively. Could you implement both?

    翻转链表,非常经典的一道题目,不难,但是细节问题需要注意,这里用三种方法来做,前两种是迭代,最后一种是递归,个人认为第一种方法最好。

    方法一:利用pre指针,追个翻转节点

    该方法从头到尾遍历链表,逐个翻转节点就行,易错点在于返回值一定是pre,而且pre的初始化要是null,这里就是反转之后链表的末尾,第一次我就写成了cur

    代码如下:

     1 /**
     2  * Definition for singly-linked list.
     3  * struct ListNode {
     4  *     int val;
     5  *     ListNode *next;
     6  *     ListNode(int x) : val(x), next(NULL) {}
     7  * };
     8  */
     9 class Solution {
    10 public:
    11     ListNode* reverseList(ListNode* head) {
    12         if (!head || !head->next) 
    13             return head;
    14         ListNode *pre = nullptr, *cur = head;  //初始化一样很重要
    15         while (cur)
    16         {
    17             ListNode *t = cur->next;
    18             cur->next = pre;
    19             pre = cur;
    20             cur = t; 
    21         }
    22         //return cur;
    23         return pre;
    24     }
    25 };

    时间复杂度:O(n)

    空间复杂度:O(1)

    方法二:

    设置虚拟头结点,每次在虚拟节点之后插入cur节点,最后直接返回dummy->next

     1 /**
     2  * Definition for singly-linked list.
     3  * struct ListNode {
     4  *     int val;
     5  *     ListNode *next;
     6  *     ListNode(int x) : val(x), next(NULL) {}
     7  * };
     8  */
     9 class Solution {
    10 public:
    11     ListNode* reverseList(ListNode* head) {
    12         if (!head || !head->next) 
    13             return head;
    14         ListNode *dummy = new ListNode(-1);
    15         ListNode *pre = dummy;
    16         ListNode *cur = head;
    17         dummy->next = head;
    18         while (cur && cur->next)
    19         {
    20             ListNode *t = cur->next;
    21             cur->next = t->next;
    22             t->next = pre->next;  //这里注意  一定不要写成t->next = cur;这么做只有第一次交换是对的之后都不对
    23             pre->next = t;
    24         }
    25         return dummy->next;
    26     }
    27 };

    时间复杂度:O(n)

    空间复杂度:O(1)

    方法三:递归的方法,每次翻转当前节点之后的所有节点,注意递归之后的操作以及最后的终止条件的确定

     1 /**
     2  * Definition for singly-linked list.
     3  * struct ListNode {
     4  *     int val;
     5  *     ListNode *next;
     6  *     ListNode(int x) : val(x), next(NULL) {}
     7  * };
     8  */
     9 class Solution {
    10 public:
    11     ListNode* reverseList(ListNode* head) {
    12         if (!head || !head->next)
    13             return head;
    14         ListNode *newHead = reverseList(head->next);
    15         head->next->next = head;
    16         head->next = nullptr;
    17         return newHead;
    18     }
    19 };

    时间复杂度:O(n)

    空间复杂度:O(n)

    参考连接:

    https://leetcode.com/problems/reverse-linked-list/solution/

    https://leetcode.com/problems/reverse-linked-list/discuss/58130

  • 相关阅读:
    省选模拟24 题解
    省选模拟23 题解
    省选模拟22 题解
    省选模拟21 题解
    省选模拟20 题解
    省选模拟19 题解
    省选模拟18 题解
    源码分析工具
    深入理解js的变量提升和函数提升
    python并发编程之IO模型
  • 原文地址:https://www.cnblogs.com/dapeng-bupt/p/8278482.html
Copyright © 2011-2022 走看看