leetcode_25题——Reverse Nodes in k-Group (链表)

Reverse Nodes in k-Group

Total Accepted: 34390 Total Submissions: 134865My Submissions

Question Solution

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

Hide Tags

Linked List

Have you met this question in a real interview?

Yes

这道题可以采用一种循环递归的方法来做,每次逆序k个节点,然后对于后面的由于和前面的一样,所以可以采用递归的方法

#include<iostream>
using namespace std;

struct ListNode {
	int val;
	ListNode *next;
	ListNode(int x) : val(x), next(NULL) {}
};

ListNode* reverseKGroup(ListNode* head, int k) {
	if(k<=1||head==NULL)
		return head;
	ListNode *ptr0=head;
	ListNode *ptr1=head;
	ListNode *head1=NULL;
	ListNode *ptr2=NULL;

	int N=k-1;
	int flag=0;
	while(N--)
	{
		ptr1=ptr1->next;
		if(ptr1==NULL)
		{
			flag=1;
			break;
		}
	}
	if(flag==1)
		return ptr0;
	ptr2=ptr1->next;

	ListNode *ptr3=ptr0->next;
	if(ptr3==ptr1)
	{
		ptr1->next=ptr0;
		ptr0->next=reverseKGroup(ptr2,k);
		return ptr1;
	}
	ListNode *ptr4=NULL;
	ptr0->next=reverseKGroup(ptr2,k);
	while(1)
	{
		ptr4=ptr3->next;
		ptr3->next=ptr0;
		if(ptr4==ptr1)
			break;
		else
		{
			ptr0=ptr3;
			ptr3=ptr4;
		}
	}
	ptr1->next=ptr3;
	return ptr1;
}

int main()
{

}

  

时间: 2025-01-07 12:31:04

leetcode_25题——Reverse Nodes in k-Group (链表)的相关文章

leetcode第24题--Reverse Nodes in k-Group

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,

leetcode_92题——Reverse Linked List II(链表操作)

Reverse Linked List II Total Accepted: 40420 Total Submissions: 154762My Submissions Question Solution Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, retur

[LintCode] 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 nod

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 valu

K组翻转链表 &#183; Reverse Nodes in

[抄题]: 给你一个链表以及一个k,将这个链表从头指针开始每k个翻转一下.链表元素个数不是k的倍数,最后剩余的不用翻转. [思维问题]: [一句话思路]: // reverse head->n1->..->nk->next.. // to head->nk->..->n1->next.. // return n1 每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. 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刷题笔记】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