[LeetCode]77. 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?

Subscribe to see which companies asked this question

解法1:一个最简单的办法就是借助栈的后进先出功能,先扫描一遍链表保存每个节点的值,然后再从头到尾遍历,将栈中元素值一一赋给链表节点。时空复杂度都是O(n)。

/**
 * 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) {
        stack<int> elem;
        ListNode* curr = head;
        while(curr != NULL) {
            elem.push(curr->val);
            curr = curr->next;
        }
        curr = head;
        while(curr != NULL) {
            curr->val = elem.top();
            curr = curr->next;
            elem.pop();
        }
        return head;
    }
};

解法2:可以做到in-place的反转。链表反转后,实际上只是中间节点的指针反转,并且反转后原来链表的头结点的下一个节点应该为NULL,而反转后链表的头结点为原来链表的尾节点。我们可以从头结点开始,每次处理两个节点之间的一个指针,将其反转过来。然后再处理接下来两个节点之间的指针……直至遇到尾节点,设置为新链表的头结点即可。

/**
 * 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* rHead = NULL; // 反转后的头节点
        ListNode* curr = head; // 当前处理节点
        ListNode* pTail = NULL; // 反转后尾节点
        while(curr != NULL) {
            ListNode* pNext = curr->next;
            if(pNext == NULL)
                rHead = curr;
            curr->next = pTail;
            pTail = curr;
            curr = pNext;
        }
        return rHead;
    }
};

上面的是一个循环来进行反转。递归的方式如下:

/**
 * 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 == NULL || head->next == NULL) return head;
        ListNode* rHead = reverseList(head->next); // 反转得到新链表的头节点
        head->next->next = head; // 当前节点的下一个节点的next指针反转过来
        head->next = NULL; // 设置新链表的尾节点
        return rHead;
    }
};
时间: 2024-10-16 21:42:35

[LeetCode]77. Reverse Linked List反转链表的相关文章

leetCode 206. Reverse Linked List 反转链表

206. Reverse Linked List Reverse a singly linked list. 反转一个链表. 思路: 采用头插法,将原来链表重新插一次返回即可. 代码如下: /**  * Definition for singly-linked list.  * struct ListNode {  *     int val;  *     ListNode *next;  *     ListNode(int x) : val(x), next(NULL) {}  * };

[LeetCode] 206. Reverse Linked List ☆(反转链表)

Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL    输出: 5->4->3->2->1->NULL 进阶: 你可以迭代或递归地反转链表.你能否用两种方法解决这道题? 解析 设置三个节点pre.cur.next (1)每次查看cur节点是否为NULL,如果是,则结束循环,获得结果 (2)如果cur节点不是为NULL,则先设置临时变量next为cur的下一个节点 (3)让cur

(Java) LeetCode 206. Reverse Linked List —— 反转链表

Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both? 原文地址:https://www.cnblogs.com/

leetcode -day30 Reverse Linked List II

1.  Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the follow

LeetCode OJ - Reverse Linked List II

题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ l

【LeetCode】Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

leetCode 92.Reverse Linked List II (反转链表II) 解题思路和方法

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

[LeetCode] 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? 之前做到Reverse Linked List II 倒置链表之二的时候我还纳闷怎么只有二没有一呢,原来真是忘了啊,现在才加上,这道题跟之前那道比起来简单不少,题目为了增加些许难度,让我们分别用

[LeetCode][JavaScript]Reverse Linked List

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? https://leetcode.com/problems/reverse-linked-list/ 反转链表. 1 /** 2 * Definition