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
     
  • 相关阅读:
    firewalld防火墙
    MariaDB
    if,for,while
    自定义带图标input样式
    display:inline-block 和 float 水平排列区别?
    css中块元素和行内元素区别
    margin-top 为什么会影响父元素的 margin-top
    vertical-align 的理解
    什么是HTTP协议?
    Grunt 自动编译 Less 文件配置
  • 原文地址:https://www.cnblogs.com/z1014601153/p/13955630.html
Copyright © 2011-2022 走看看