leetCode 25.Reverse Nodes in k-Group (以k个节点为一组反转链表) 解题思路和方法

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

思路:此题是有点繁琐的,数据结构在链表的学习上本来就有些薄弱,此题调试了很久。整体思路是把所有的链表全部按照k个节点一组,反转,同时记录反转的数目num,到k之后重新计数,反转完毕之后,查看num数值,大于初始值,则将最后一组的链表再反转回来即可。代码如下:

    public ListNode reverseKGroup(ListNode head, int k) {
        if(k < 2 || head == null){
            return head;
        }

        int num = 1;//计数
        ListNode pre = new ListNode(0);//定义头结点
        pre.next = head;
        ListNode firstNode = pre;//保存头结点
        ListNode next = null;//下一个节点
        ListNode cur = head.next;//当前节点
        ListNode curHead = head;//不随位置改变的头结点(如1,2,3),curHead =1;变为321后,curHead仍=1
        while(cur != null){//当前节点不为null
            next = cur.next;//保存next节点
            //交换节点
            pre.next = cur;
            cur.next = head;
            curHead.next = next;//节点交换完成
            //如果next==null,num+1与k比较后返回
            if(next == null){
            	if(++num == k){
            		num = 1;
            	}
                break;
            }
            //为下一循环准备
            head = cur;
            cur = next;
            //如果已交换的节点==k,则pre等节点改变位置
            if(++num == k){
                num = 1;
                pre = curHead;
                curHead = cur;
                head = cur;
                cur = cur.next;
            }
        }
        //如果num>1表明最后不够k的节点也交换了,交换回来即可
        if(num > 1){
        	curHead = head = pre.next;
	        cur = head.next;
	        while(cur != null){//与上述代码类似
	        	next = cur.next;
	        	//交换数据
	        	pre.next = cur;
	        	cur.next = head;
	        	curHead.next = next;
	        	//更新数据,准备下一循环
	        	head = cur;
	        	cur = next;
	        }
        }
        return firstNode.next;
    }

不过这样写,代码上还是有些复杂了,凌乱了一些。觉得应该会有更好的写法,再次参考下别人的资料,递归调用是个不错的方法。借鉴之后写出简洁的代码如下:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
	    public ListNode reverseKGroup(ListNode head, int k) {
	        if(k < 2 || head == null){
	            return head;
	        }
	        int num = k;
	        ListNode p = head;
	        //判断节点个数与k的大小
	        while(num > 1 && p != null){
	            p = p.next;
	            num--;
	        }
	        if(p == null){//说明k比节点的个数大,直接返回
	            return head;
	        }
	        num = k;//再次赋初始值
	        p = head;
	        ListNode q = null;//定义两个临时周转节点
	        ListNode r = null;
	        while(num > 1){
	            q = p.next;//保存变量
	            r = q.next;
	            //交换节点
	            q.next = head;
	            p.next = r;
	            //变量前进
	            head = q;
	            num--;
	        }
	        p.next = reverseKGroup(p.next,k);//递归调用
	        return head;
	    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-25 21:43:05

leetCode 25.Reverse Nodes in k-Group (以k个节点为一组反转链表) 解题思路和方法的相关文章

(Java) LeetCode 25. Reverse Nodes in k-Group —— k个一组翻转链表

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

LeetCode 25 Reverse Nodes in k-Group K个一组反转节点

题目: 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

[LeetCode] 25. Reverse Nodes in k-Group 每k个一组翻转链表

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

[LeetCode]25. Reverse Nodes in k-Group k个一组翻转链表

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

leetcode 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. 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 nod

Java [leetcode 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. 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, on

LeetCode 25 Reverse Nodes in k-Group Add to List 划分list

题目链接: https://leetcode.com/problems/reverse-nodes-in-k-group/?tab=Description Problem :将一个有序list划分为k个组,并且每个组的元素逆置 链表操作 :递归算法 每次寻找到该组的尾部,然后进行逆置操作,返回头部,这样每次递归操作之后能够进行下一次的逆置操作. 链表操作画图比较形象!!!! 对于递归算法:找到共同点,找到程序退出点,注意特殊情况 本题中,共同点为每组为k个节点,并且每组进行的操作均为逆置操作.

[LeetCode] 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

LeetCode 25 Reverse Nodes in k-Group (C,C++,Java,Python)

Problem: 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,