leetcode 25. Reverse Nodes in k-Group & leetcode 92. Reverse Linked List II

来源:互联网 发布:centos怎么下载 编辑:程序博客网 时间:2024/05/29 13:36

写在前面

链表的题目可以说是非常恶心了,尤其是这类要求奇葩的题目,当然能够AC本题也算是链表操作熟练的一种体现吧,本题我的解法基本是反转链表方法的一种变形,思路较为清晰。

leetcode 25. Reverse Nodes in k-Group

题目描述

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

You may not alter the values in the nodes, only nodes itself may be changed.

Only constant memory is allowed.

For example,
Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5

解题思路

简单点说就是每隔K个数进行一次链表反转,如果剩余的数不足k,则不作任何操作。首先我们面对这种题目,有间隔的链表操作,一般都要新建一个头结点,这样方便我们在循环中的赋值。本题如果没有新建头结点,在循环内的操作基本上是没法做的,思路并不难,循环体内部就是不断在一个区间内做链表反转,更新下次反转的开始和结束位置,关键是自己要动手写出来。

AC解

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode* reverseKGroup(ListNode* head, int k) {        // constant memory        typedef ListNode* Node;        if(head == nullptr) return head;        Node dummy = new ListNode(0);        dummy->next = head;        int num = 0;        auto cur = head;        while(cur) {            num++;            cur = cur->next;        }        auto begin = dummy;        while(num) {            int count = 0;            auto end = begin->next;            while(count<k) {                count++;                end = end->next;                if(end == nullptr) break;            }            if(count!=k) break;            // else from begin to end            auto first = begin->next;            auto ft = first;            auto second = first->next;            while(second!=end) {                auto temp = second->next;                second->next = first;                first = second;                second = temp;            }            begin->next = first;            begin = ft;            begin->next = end;            num-=k;        }        return dummy->next;    }};

leetcode 92. Reverse Linked List II

题目描述

Reverse a linked list from position m to n. Do it in-place and in one-pass.

For example:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.

思路分析

本题基本上是上题的简化版本,把其中reverse的部分拿出来就是本题的解了,具体如下。

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode* reverseBetween(ListNode* head, int m, int n) {        // 跟之前做的题一个鬼样        typedef ListNode Node;        auto dummy = new Node(0);        dummy->next = head;        auto cur = head;        int i = 1;        auto start = dummy;        while(i<m) {            start = start->next;            if(start == nullptr) return dummy->next;            i++;        }        // 现在start指向m的前一个元素        auto end = start->next;        while(i<=n) {            end = end->next;            if(end == nullptr) break;            ++i;        }        // end 指向n后一个元素        // 开始reverse        auto first = start->next;        auto pre = first;        auto second = first->next;        while(second!=end) {            auto temp = second->next;            second->next = first;            first = second;            second = temp;        }        start->next = first;        pre->next = second;        return dummy->next;    }};
原创粉丝点击