zoukankan      html  css  js  c++  java
  • 25. Reverse Nodes in k-Group

    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 
    

    解析

    • 自己的思路虽然AC过了,但是不容易懂!
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* reverseList(ListNode* first, ListNode* tail)
    	{
    		ListNode* pre = NULL;
    		ListNode* cur = first;
    		ListNode* next = NULL;
    		while (cur != tail)
    		{
    			next = cur->next;
    			cur->next = pre; //第一个节点就为空啊!!!
    
    			pre = cur;
    			cur = next;
    		}
    		return first;
    	}
    	ListNode* reverseKGroup(ListNode* head, int k) {
    
    		if (k < 2 || !head)
    		{
    			return head;
    		}
    		ListNode* tail = NULL;
    		ListNode* first = head;
    
    		ListNode* cur = head;
    		ListNode* newHead = NULL;
    
    		ListNode* ret = NULL;
    		ListNode* pre = NULL;
    
    		int cnt = k;
    		while (cur)
    		{
    
    			while (cur&&cnt--)
    			{
    				/*if (cur== NULL)
    				{
    				break;
    				}*/
    				pre = cur; //
    				cur = cur->next;
    			}
    			if (cnt > 0)
    			{
    				break;
    			}
    			if (ret)
    			{
    				ret->next = pre; // 反转链表的尾节点直接挂下一轮k个节点的尾节点
    			}
    			ret = reverseList(first, cur); //cur下一次的起点,不动;返回反转后的尾节点
    
    			ret->next = cur; //反转链表的尾节点直接挂下一轮k个节点的开始;防止下一轮没有k个节点
    			first = cur;
    
    			cnt = k;
    		}
    
    		if (!newHead)
    		{
    			return head;
    		}
    
    		return newHead;
    	}
    };
    
        ListNode* reverseKGroup(ListNode* head, int k) {
            if(!head) return NULL;
            ListNode* dummy = new ListNode(0);
            ListNode* cur = head;
            ListNode* pre = NULL;
            ListNode* last_tail = dummy;
            int num = 0;
            while(cur){
                cur = cur->next;
                num++;
            }
            cur = head;
            while(num >= k){
                ListNode* tail = cur;
                for(int i = 0; i < k; i++){
                    ListNode* temp = cur->next;
                    cur->next = pre;
                    pre = cur;
                    cur = temp;
                }
                last_tail->next = pre;
                last_tail = tail;
                num -= k;
            }
            last_tail->next = cur;
            return dummy->next;
        }
    
    

    题目来源

  • 相关阅读:
    题解-CF1375E Inversion SwapSort
    寒门再难出贵子
    js获取链接中的内容方法
    MySQL添加用户、删除用户、授权及撤销权限
    Ubuntu保存退出vim编辑器
    最全!Linux服务器下安装SVN,并添加SVN项目,自动更新项目文件到web目录
    php $_SERVER中的SERVER_NAME 和HTTP_HOST的区别以及REQUEST_URI的讲解
    RESTful API 最佳实践----转载阮一峰
    PHP图像处理(GD库)
    nginx.conf配置
  • 原文地址:https://www.cnblogs.com/ranjiewen/p/8351325.html
Copyright © 2011-2022 走看看