# 链表以k个一组进行翻转（2014美团研发笔试）

ListNode *reverseKGroup(ListNode *head, int k)
{
if (head == nullptr || head->next == nullptr || k < 2)
for (int i = 0; i < k; ++i)
{
if (next_group)
next_group = next_group->next;
else
}
// next_group is the head of next group
// new_next_group is the new head of next group after reversion
ListNode *new_next_group = reverseKGroup(next_group, k);
ListNode *prev = NULL, *cur = head;
while (cur != next_group)
{
ListNode *next = cur->next;
cur->next = prev ? prev : new_next_group;
prev = cur;
cur = next;
}
return prev; // prev will be the new head of this group
}


/* Reverses the linked list in groups of size k and returns the
pointer to the new head node. */
struct node *reverse (struct node *head, int k)
{
struct node* next;
struct node* prev = NULL;
int count = 0;

/*reverse first k nodes of the linked list */
while (current != NULL && count < k)
{
next  = current->next;
current->next = prev;
prev = current;
current = next;
count++;
}

/* next is now a pointer to (k+1)th node
Recursively call for the list starting from current.
And make rest of the list as next of first node */
if(next !=  NULL)
{
}

/* prev is new head of the input list */
return prev;
}


• 本文已收录于以下专栏：

## 单链表中k个结点一组进行反转

【025-Reverse Nodes in k-Group（单链表中k个结点一组进行反转）】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题　　Given a linked l...

## 二叉树的高度 、销毁、链表的翻转（k为一组、n-m 之间）两种实现、

1.二叉树的高度：//实现思路：左右子树较高的一支，将其结果结+1就是当前树的高度 size_t GetHeight(Node* pRoot) { if (pRoot) { ...

## [经典面试题]k节点一组旋转链表

【题目】 给出一个链表和一个数k，比如链表1→2→3→4→5→6，k=2，则翻转后2→1→4→3→6→5，若k=3,翻转后3→2→1→6→5→4，若k=4，翻转后4→3→2→1→5→6。 如果节点的数...

## 链表——按k个结点一组来反转链表

举报原因： 您举报文章：链表以k个一组进行翻转（2014美团研发笔试） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)
The provided text is a code snippet for reversing a linked list in groups of size k. It does not contain any mathematical questions or answers.
no_question
Given a text passage, determine:
    1. If it contains a mathematical question (including word problems or calculations)
    2. If an answer to that mathematical question is provided in the text

First, describe the content structure you found in 1-2 sentences.

Then provide your analysis in the following format:
TYPE: <one of the following values>
- NO_QUESTION
- SINGLE_QUESTION
- SINGLE_QUESTION_WITH_ANSWER
- MULTIPLE_QUESTIONS
- MULTIPLE_QUESTIONS_WITH_ANSWERS
system
# 链表以k个一组进行翻转（2014美团研发笔试）

ListNode *reverseKGroup(ListNode *head, int k)
{
if (head == nullptr || head->next == nullptr || k < 2)
for (int i = 0; i < k; ++i)
{
if (next_group)
next_group = next_group->next;
else
}
// next_group is the head of next group
// new_next_group is the new head of next group after reversion
ListNode *new_next_group = reverseKGroup(next_group, k);
ListNode *prev = NULL, *cur = head;
while (cur != next_group)
{
ListNode *next = cur->next;
cur->next = prev ? prev : new_next_group;
prev = cur;
cur = next;
}
return prev; // prev will be the new head of this group
}


/* Reverses the linked list in groups of size k and returns the
pointer to the new head node. */
struct node *reverse (struct node *head, int k)
{
struct node* next;
struct node* prev = NULL;
int count = 0;

/*reverse first k nodes of the linked list */
while (current != NULL && count < k)
{
next  = current->next;
current->next = prev;
prev = current;
current = next;
count++;
}

/* next is now a pointer to (k+1)th node
Recursively call for the list starting from current.
And make rest of the list as next of first node */
if(next !=  NULL)
{
}

/* prev is new head of the input list */
return prev;
}


• 本文已收录于以下专栏：

## 单链表中k个结点一组进行反转

【025-Reverse Nodes in k-Group（单链表中k个结点一组进行反转）】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题　　Given a linked l...

## 二叉树的高度 、销毁、链表的翻转（k为一组、n-m 之间）两种实现、

1.二叉树的高度：//实现思路：左右子树较高的一支，将其结果结+1就是当前树的高度 size_t GetHeight(Node* pRoot) { if (pRoot) { ...

## [经典面试题]k节点一组旋转链表

【题目】 给出一个链表和一个数k，比如链表1→2→3→4→5→6，k=2，则翻转后2→1→4→3→6→5，若k=3,翻转后3→2→1→6→5→4，若k=4，翻转后4→3→2→1→5→6。 如果节点的数...

## 链表——按k个结点一组来反转链表

举报原因： 您举报文章：链表以k个一组进行翻转（2014美团研发笔试） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)
user
The provided text is a code snippet for reversing a linked list in groups of size k. It does not contain any mathematical questions or answers.

TYPE: NO_QUESTION