zoukankan      html  css  js  c++  java
  • [LeetCode] 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.

    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

    思路: 先实现reverseList(head), 然后实现reverse(start, end)反正从start到end的链表,然后基于reverse(start,end)来完成最后的动作。

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
        public:
            ListNode *reverse(ListNode* head)
            {
                if(head == NULL)
                    return NULL;
                ListNode * pre = NULL;
                ListNode * cur = head;
                ListNode * next = NULL;
    
                // make cur->next = pre;
                while(cur)
                {
                    next = cur->next;
                    cur->next = pre;
    
                    pre = cur;
                    cur = next;
                }
                return pre;
    
            }
    #if 1
            ListNode* reverse(ListNode* head, ListNode* end)
            {
                if(head == NULL)
                    return NULL;
                ListNode * pre = NULL;
                ListNode * cur = head;
                ListNode * next = NULL;
    
                // make cur->next = pre;
                while(cur)
                {
                    next = cur->next;
                    cur->next = pre;
    
                    pre = cur;
                    cur = next;
    
                    if(pre == end)
                        break;
                }
                return pre;
    
            }
            ListNode *reverseKGroup(ListNode *head, int k)
            {
                if(head == NULL)
                    return NULL;
    
                ListNode dummy(-1);
                dummy.next = head;
    
                ListNode* pre = &dummy;
                ListNode* start= head;
                ListNode* end = start;
                ListNode* next= NULL;
                int cnt = 0;
    
                while(end)
                {
                    cnt++;
                    if(cnt == k)
                    {
                        next = end->next;
                        //assign
                        pre->next = reverse(start, end);
                        start->next = next;
                        //cout << "start	" << start->val <<endl;
                        //cout << "end	" << end->val <<endl;
                        //printList(dummy.next);
                        //printList(pre->next);
    
                        //update
                        pre = start;
                        start = next;
                        end = next;
                        cnt = 0;
                        //cout << "start	" << start->val <<endl;
                        //cout << "end	" << end->val <<endl;
                        //cout << "pre	" << pre->val <<endl;
    
                    }
                    else
                        end = end->next;
                }
                return dummy.next;
            }
    #endif
    };
  • 相关阅读:
    其实说起来要国庆节回去,心里有点担心再最后买不到票
    现在快要中秋节,之后又是国庆节
    天气开始降温,今天会下雨
    昨天晚上控制家里的电脑,与家里人视频
    有时候手机打开微信或者其它应用真的是非常慢
    手机有时候卡的不行,还是自己照顾着使用吧
    这几天晚上都是再看小别离,还是有点意思
    函数初识
    Python文件操作
    Python字符编码
  • 原文地址:https://www.cnblogs.com/diegodu/p/4280031.html
Copyright © 2011-2022 走看看