《剑指offer》24--反转链表[C++]

NowCoder《剑指offer》24--反转链表icon-default.png?t=M666https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId=11168&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking 

题目描述

输入一个链表,反转链表后,输出新链表的表头。

struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) : val(x), next(NULL) {}
};

解题思路

 1 用栈

class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        if(pHead == NULL || pHead -> next == NULL) return pHead;
        ListNode* p = pHead;
        stack<ListNode* > s;
        while(p -> next) {
            s.push(p);
            p = p -> next;
        }
        ListNode* head = p;
        while(!s.empty()) {
            p -> next = s.top();
            p = p -> next;
            s.pop();
        }
        p -> next = NULL;
        return head;
    }
};

2 新建链表

class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        if(pHead == NULL || pHead -> next == NULL) return pHead;
        ListNode *reHead = NULL, *cur = pHead, *tmp = NULL,*pre = NULL;
        while(cur != NULL) {
            tmp = cur->next;
            cur->next = pre;
            if(tmp == NULL) reHead = cur;
            pre = cur;
            cur = tmp;
        }
        return reHead;
    }
};

3 头结点插入法

class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        if(pHead == NULL || pHead -> next == NULL) return pHead;
        ListNode *dummy = new ListNode(-1);
        ListNode *pCur = pHead, *pNex;
        while(pCur != NULL) {
            pNex = pCur->next;
            pCur->next = dummy->next;
            dummy->next = pCur;
            pCur = pNex;
        }
        return dummy->next;
    }
};

4 就地反转

class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        if(pHead == NULL || pHead -> next == NULL) return pHead;
        ListNode *dummy = new ListNode(-1);
        dummy->next = pHead;
        ListNode *prev = dummy->next;
        ListNode *pCur = prev->next;
        while(pCur != NULL) {
            prev->next = pCur->next;
            pCur->next = dummy->next;
            dummy->next = pCur;
            pCur = prev->next;
        }
        return dummy->next;
    }
};

5 递归:leetcode 206 答案

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head == NULL || head->next==NULL) return head;
        ListNode *n = reverseList(head->next);
        head->next->next = head;
        head->next = NULL;
        return n;
    }
};;

LeetCode-206. Reverse Linked List [C++][Java]_贫道绝缘子的博客-CSDN博客Given theheadof a singly linked list, reverse the list, and returnthe reversed list.https://blog.csdn.net/qq_15711195/article/details/126133049?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22126133049%22%2C%22source%22%3A%22qq_15711195%22%7D&ctrtid=rIJLy​​​​​​​

参考文献

[1] LeetCode 206.反转链表4种方法图解(①就地反转②头插法③迭代法④递归法)Java语言_棒棒的小笨笨的博客-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贫道绝缘子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值