[英]Reversing every k nodes of a linked list
I have two implementation of reversing a linked list. 我有两种反向链接列表的实现。 The first one is defined as Node *Reverse(Node *head, int k) method.
第一个定义为Node * Reverse(Node * head,int k)方法。 This method reverses every k alternate sub groups of a linked list.
此方法反转链表的每k个备用子组。
Example: Inputs: 1->2->3->4->5->6->7->8->NULL and k = 3
Output: 3->2->1->6->5->4->8->7->NULL
The other implementation is defined as kAltReverse(Node *head, int k). 另一个实现定义为kAltReverse(Node * head,int k)。 This function reverses every k node but then skips the next k node and does the same for the next k node.
此函数反转每个k节点,然后跳过下一个k节点,并对下一个k节点执行相同的操作。
Example Input: 1->2->3->4->5->6->7->8->9->NULL and k = 3
Output: 3->2->1->4->5->6->9->8->7->NULL
Here is my code with definition of the Node structure and the two functions Reverse and kAltReverse 这是我的代码,其中定义了Node结构以及两个函数Reverse和kAltReverse
// This is the definition of node structure
typedef struct container{
int data;
struct container *next;
} Node;
Node *reverse(Node *head, int k){
Node *temp = head;
Node *curr = head;
Node *prev = NULL;
Node *next = NULL;
int count = 0;
// Reverses the first k nodes iteratively
while (curr != NULL && count < k){
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
count++;
}
// Recursively linking the head of the list to next reversed list.
if (next != NULL) temp->next = reverse(next,k);
return prev;
}
Node *kAltReverse(Node *head, int k){
Node *temp = head;
Node *curr = head;
Node *prev = NULL;
Node *next = NULL;
int count = 0;
// Reverse the first k node of the linked list
while (curr != NULL && count < k){
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
count++;
}
// Now head points to the kth node. So change next of head to (k+1)th node
if (head != NULL) temp->next = curr;
count = 0;
//Move the pointer node so as to skip next k nodes.
while(curr != NULL && count < k-1){
curr = curr->next;
count++;
}
// Recursively call for the list starting from curr->next.
// And make rest of the list as next of first node.
if (curr != NULL) curr->next = kAltReverse(curr->next,k);
return prev;
}
int main(){
Node *head1 = NULL;
/* Insert function is a function for pushing the element in stack
like fashion on to the list*/
insertFirst(&head1, 6);
insertFirst(&head1, 4);
insertFirst(&head1, 3);
insertFirst(&head1, 2);
insertFirst(&head1, 1);
insertFirst(&head1, 5);
// So the list will be 5->1->2->3->4->6->NULL
// Now when I call the functions Reverse and kAltReverse I get undesired
// output.
printlist(head1);
Node *Reverse = reverse(head1, 2);
printlist(Reverse);
Node *kAlt1 = kAltReverse(head1,2);
printlist(kAlt1);
return 0;
}
The output which I get is: 我得到的输出是:
5 1 2 3 4 6 // This is the original list
1 5 3 2 6 4 // This is the output given by Reverse method
3 5 2 6 4 // This is the output given by kAltReverse method
But if I call them separately and comment out the other method, I get desired output ie 但是,如果我分别调用它们并注释掉另一种方法,则会得到所需的输出,即
Output from reverse: 1 5 3 2 6 4 // This is correct
Output form kAltReverse as: 1 5 2 3 6 4 // This is correct too.
So they work separately but not together. 因此,它们是分开工作,而不是一起工作。
I am unable to figure out why this happens when both are called simultaneously. 我无法弄清楚为什么同时调用两者时会发生这种情况。 On the other hand when these methods are called independent of each other they give proper output.
另一方面,当这些方法彼此独立地调用时,它们会提供适当的输出。 Please help.
请帮忙。
You don't change head
, but you do change (reorder) the interior nodes inside the list. 您无需更改
head
,但可以更改(重新排序)列表内的内部节点。 Try calling printlist(head1)
after calling reverse
and you will see it. 尝试
reverse
调用后调用printlist(head1)
,您会看到它。 – Some programmer dude –一些程序员
Using a call by value does not create a copy of the whole linked linked list. 按值使用调用不会创建整个链接链表的副本。 It just works on a copy of the head node.
它仅适用于头节点的副本。 In the frame of the function
reverse(head1, 2);
在函数的框架中
reverse(head1, 2);
, a copy of head1
is used in the context of the function and head1
is left unchanged. ,则在该函数的上下文中使用
head1
的副本,并且head1
保持不变。 Let's call this copy head1f
. 我们将此副本
head1f
。 If head1f.data=42
is called, head1.data
is unchanged. 如果
head1f.data=42
,则head1.data
不变。 But if head1f.next->data=42
is used instead, head1.next->data
is now 42 : head1f.next
and head1.next
points to the same Node
. 但是,如果
head1f.next->data=42
,则head1.next->data
现在为42: head1f.next
和head1.next
指向同一Node
。 – francis –弗朗西斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.