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;
        }
    
    

    题目来源

  • 相关阅读:
    linux查看CPU和内存信息
    linux yum命令详解
    查看文件中关键字前后几行的内容
    vue.js+web storm安装及第一个vue.js
    android GPS: code should explicitly check to see if permission is available
    ASP.NET MVC Identity 使用自己的SQL Server数据库
    阿里云服务器,tomcat启动,一直卡在At least one JAR was scanned for TLDs yet contained no TLDs就不动了
    ASP.NET MVC4 MVC 当前上下文中不存在名称“Scripts”
    python 将windows字体中的汉字生成图片的方法
    Java android DES+Base64加密解密
  • 原文地址:https://www.cnblogs.com/ranjiewen/p/8351325.html
Copyright © 2011-2022 走看看