• 链表翻转题目整理


    206. Reverse Linked List

    【题意】

    将整个链表进行翻转

    【题解】

    非递归的做法比较好理解。

    递归的做法很巧妙。用tail指针保存最后一个节点,这个不难理解,主要是head->next->next = head ,假设链表为1->2->3->4, 当head->3时,head->next->next = head即3<-4,然后

    head->next = NULL就把3指向4的指针删去了,这样递归下去,最后会得到1->(2<-3<-4<-tail),此时按照上述方法就变成了NULL<-1<-2<-3<-4<-tail,然后返回tail即可。

    【代码】

    非递归:

    /**
     * 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 *pre = NULL;
            ListNode *next = NULL;
            while(head != NULL){
                next = head->next;
                head->next = pre;
                pre = head;
                head = next;
            }
            return pre;
        }
    };
    View Code

    递归:

    /**
     * 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 *tail = reverseList(head->next);
            head->next->next = head;
            head->next = NULL;
            return tail;
        }
    };
    View Code

    92. Reverse Linked List II

    【题意】

    将位置处于[m, n]之间的节点进行翻转

    【题解】

    感觉这个题解讲的很棒

    【代码】

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode *t, *last;
        ListNode* reverseN(ListNode* head, int n){
            if (n == 1){
                t = head->next;
                return head;
            }
            last = reverseN(head->next, n - 1);
            head->next->next = head;
            head->next = t;
            return last;
        }
        ListNode* reverseBetween(ListNode* head, int m, int n) {
            if (m == 1){
                return reverseN(head, n);
            }
            head->next = reverseBetween(head->next, m - 1, n - 1);
            return head;
        }
    };
    View Code

    25. Reverse Nodes in k-Group

    【题意】

    每k个数字进行翻转

    【代码】

    /**
     * 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* reverseKGroup(ListNode* head, int k) {
            if (head == NULL || head->next == NULL)return head;
            ListNode *tail = head;
            for (int i = 0; i < k; i++){
                if (tail == NULL)return head;
                tail = tail->next;
            }
            ListNode *p = reverse(head, tail);
            head->next = reverseKGroup(tail, k);
            return p;
        }
        ListNode* reverse(ListNode *head, ListNode *tail){
            ListNode *pre = NULL;
            ListNode *next = NULL;
            while(head != tail){
                next = head->next;
                head->next = pre;
                pre = head;
                head = next;
            }
            return pre;
        }
    };
    View Code
     
  • 相关阅读:
    剖析虚幻渲染体系(12) 移动端专题Part 1(UE移动端渲染分析)
    剖析虚幻渲染体系(13) RHI补充篇:现代图形API之奥义与指南
    剖析虚幻渲染体系(12) 移动端专题Part 3(渲染优化)
    浏览器无环境调试
    RPC调用获取参数值
    vscode插件
    前端异常收集和处理
    互联网名词集锦
    今日思考20211104
    备忘项目进展萃取
  • 原文地址:https://www.cnblogs.com/z1014601153/p/13955630.html
Copyright © 2020-2023  润新知