链表翻转题目整理

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

 

 
posted @ 2020-11-10 19:51  牛肉叉烧饭w  阅读(151)  评论(0编辑  收藏  举报
levels of contents