zoukankan      html  css  js  c++  java
  • 【Reverse Nodes in k-Group】cpp

    题目

    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.

    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

    代码:

    /**
     * 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) {
                if ( !head || !(head->next) || k<2 ) return head;
                ListNode dummy(-1);
                dummy.next = head;
                ListNode *beginK = &dummy;
                ListNode *endK = &dummy;
                while ( true )
                {
                    // move forward k steps
                    for ( size_t i = 0; i < k && endK; ++i ) {endK = endK->next;}
                    // check if already move to the end of linked list
                    if (!endK) break;
                    // reverse from beginK to endK
                    ListNode *curr = beginK->next;
                    for ( size_t i = 0; i < k-1; ++i )
                    {
                        ListNode *tmp = curr->next;
                        curr->next = tmp->next;
                        tmp->next = beginK->next;
                        beginK->next = tmp;
                    }
                    beginK = curr;
                    endK = curr;
                }
                return dummy.next;
        }
    };

    Tips

    这道题是Reverse Linked List(http://www.cnblogs.com/xbf9xbf/p/4464896.html)的加强版。

    大概分两步:

    1. 判断这一group是否够k个元素

    2. 将这一group的k个元素reverse

    每次reverse之前,都要构造成如下的条件

    结合代码以及下面的示例:以k=3为例

    beginK→1(curr)→2→3(endK)→...

    beginK:这一group之前的那个ListNode

    curr: 这一group的第一个元素

    endK:这一group的最后一个元素

    3. 注意每次reverse完成后,迭代beginK和endK的值。

    4. 注意循环终止的条件是endK移动到的NULL。

    代码的风格追求简洁,因为简洁往往方便理解。

    =============================================

    第二次过这道题,一次AC了。这道题主要考查reverse linked list的操作。

    /**
     * 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) {
                if ( !head || k<2 ) return head;
                ListNode dummpy(-1);
                dummpy.next = head;
                ListNode* p1 = &dummpy;
                ListNode* p2 = &dummpy;
                for ( int i=0; i<k && p2; ++i ) p2 = p2->next;
                while ( p2 )
                {
                    // reverse Nodes in one group
                    ListNode* curr = p1->next;
                    for ( int i=0; i<k-1; ++i )
                    {
                        ListNode* tmp = curr->next;
                        curr->next = tmp->next;
                        tmp->next = p1->next;
                        p1->next = tmp;
                    }
                    p1 = curr;
                    p2 = curr;
                    // if existing next k-group Nodes
                    for ( int i=0; i<k && p2; ++i ) p2 = p2->next;
                }
                return dummpy.next;
        }
    };
  • 相关阅读:
    [ios] 响应上下左右滑动手势
    [ios]字符串转化成NSDate类型 计算与当前时间的相差 月数 天数 【转】
    [ios] NSSet,NSMutableSet[zhuan]
    [ios] 如何让xcode自动检查内存泄露 【转】
    iOS 使用Quartz 2D画虚线 【转】
    [ios]设置表格单元格交替背景 【转】
    [ios] IOS文件操作的两种方式:NSFileManager操作和流操作【转】
    [ios] 自定义UIAlertView样式,实现可替换背景和按钮 【转】
    [ios]上传应用程序到app store 【转】
    [ios] iOS中arc的设置与使用
  • 原文地址:https://www.cnblogs.com/xbf9xbf/p/4468268.html
Copyright © 2011-2022 走看看