zoukankan      html  css  js  c++  java
  • 链表翻转题目整理

    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
     
  • 相关阅读:
    PeerJS
    OpenEMM 2013 发布,电子邮件营销软件
    feh 2.9 发布,图像查看器
    PyPyODBC 0.9.2发布 纯Python实现的pyodbc替代库
    MariaDB 5.5.29, 5.3.12, 5.2.14, 5.1.67
    CouchDB 1.3.0的新特性以及算法的强化
    Boost 1.53.0 发布,可移植的C++标准库
    ActiveMQ 5.8.0 发布,JMS 消息服务器
    MySQL 5.5.30 发布
    CloverETL Designer 3.3.1 发布
  • 原文地址:https://www.cnblogs.com/z1014601153/p/13955630.html
Copyright © 2011-2022 走看看