LintCode 450: Reverse Nodes in k-Group (链表翻转题)

450. 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.

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.

Example

Example 1

Input:
list = 1->2->3->4->5->null
k = 2
Output:
2->1->4->3->5

Example 2

Input:
list = 1->2->3->4->5->null
k = 3
Output:
3->2->1->4->5

Input test data (one parameter per line)How to understand a testcase?

解法1:

这题感觉并不容易,比单纯的链表翻转要难很多,很多地方要考虑。

以输入为
1->2->3->4->5->null
2

为例,输出为2->1->4->3->5。

我们要考虑的地方有:

1) 将2保存起来,作为最后返回的head。
2) 1要和4连起来,所以1也要保存起来。同样,3要和5连起来。
3)  另外还要注意链表长度刚好时k的整数倍的情况,也就是3可能和后面的元素相连,也可能接NULL。

代码如下,比较冗长。应该可以优化。

/**
 * Definition of singly-linked-list:
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *        this->val = val;
 *        this->next = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param head: a ListNode
     * @param k: An integer
     * @return: a ListNode
     */
    ListNode * reverseKGroup(ListNode * head, int k) {
        if (!head) return NULL;
        int count = 0;
        ListNode * prev = NULL, * oldPrev = NULL;
        ListNode * pEndNode = head;
        bool enoughNodes = true;
        ListNode * newHead = NULL;
        while(enoughNodes) {
            count = 0;
            while(count < k) {
                if (!pEndNode) {
                    if (count != k) enoughNodes = false;
                    break;
                } else {
                    pEndNode = pEndNode->next;
                    count++;    
                }
            }
            if (enoughNodes) {
                ListNode *oldHead = head;
                ListNode * temp = NULL;
                while(head && head != pEndNode) {
                    temp = head->next;
                    head->next = prev;
                    prev = head;
                    head = temp;
                }

                if (!newHead) newHead = prev;

                oldHead->next = head;

                if (!oldPrev) { 
                    oldPrev = oldHead;
                } else {
                    oldPrev->next = prev;
                    oldPrev = oldHead;
                }
                prev = oldHead;
            }
        }
        
        return newHead;
    }
};

解法2:用递归

/**
 * 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) return NULL;
        ListNode *node = head, *origHead = head;
        for (int i = 1; i < k; i++) {
            if (node) node = node->next;            
            if (!node) {
                return head; //if < k nodes, no reverse and just returns head.
            }

        }
        ListNode *newHead = node;
        ListNode *newStart = NULL;
        if (node) newStart = node->next;
        if (node) reverseGroup(head, node->next);
        if (node) origHead->next = reverseKGroup(newStart, k);
        
        return newHead;
    }
private:
    //reverse the nodes in [start, end)
    ListNode *reverseGroup(ListNode* start, ListNode *end) {
        ListNode *pre = NULL;
        while (start != end) {
            ListNode *tmp = start->next;
            start->next = pre;
            pre = start;
            start = tmp;
        }
        return pre;
    }
};

二刷递归解法
 

/**
 * 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) {
        ListNode *a = head, *b = head;
        if (!head) return NULL;
        for (int i = 0; i < k; i++) {
            if (b == NULL) return head;
            b = b->next;
        
        }
        ListNode *newHead = reverseRange(a, b);
        a->next = reverseKGroup(b, k);
        return newHead;
    }

private:
    //reverse nodes range [a, b)
    ListNode *reverseRange(ListNode *a, ListNode *b) {
        ListNode *pre = NULL;
        while (a != b) {
            ListNode *tmp = a->next;
            a->next = pre;
            pre = a;
            a = tmp;
        }
        return pre;
    }
};

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值