给定长度为n+1的数组_使用C ++程序按给定大小的组反向链接列表

给定长度为n+1的数组

Given a linked list of size N. The task is to reverse every k nodes in the linked list.

给定大小为N的链表。 任务是反转链表中的每k个节点。

Explanation and example:

说明和示例:

    If a linked listis: 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 
    The value of k is 2
    Then the linked list looks like: 2 → 1 → 4 → 3 → 6 → 5 → 8 → 7

Algorithm:

算法:

To solve the problem we follow this algorithm,

为了解决这个问题,我们遵循此算法,

There is a function named as Reverse(start_node, k) and it reverses the k nodes from the start_node and every time it returns a starting node of that group.

有一个名为Reverse(start_node,k)的函数,它从start_node反转k个节点,每次返回该组的起始节点。

  1. We store the next pointer into node variable and connect the current pointer to the head of another node variable name as prev.

    我们将下一个指针存储到node变量中,并将当前指针连接到另一个节点变量名的开头,如prev 。

  2. In the reverse list head node of the linkedlist will come to the tail and we connect the next node to next pointer of the head.

    在反向列表中,链表的头节点将位于尾部,我们将下一个节点连接到头的下一个指针。

    head->next=Reverse(start->node,k)
    Reverse function:
    
    //base case
    if(head==NULL){
        return NULL;
    }
    
    //declarations
    struct node* next=NULL;
    struct node* prev=NULL;
    struct node* curr=head;
    int count=0;
    while(curr&& count<k){
        //we store the next pointer into next
        //and connect the  current pointer to the head of the prev
        next=curr->next;
        curr->next=prev;
        prev=curr;
        curr=next;
        count++;
    }
    //in the reverse list head node of the linked list will come to the tail
    //for this we connect the next node to it's next pointer 
    head->next=reverse(curr,k);

    return prev;

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;

struct node{
    int data;
    node* next;
};

//Create a new node
struct node* create_node(int x){
    struct node* temp= new node;
    temp->data=x;
    temp->next=NULL;
    return temp;
}

//Enter the node into the linked list
void push(node** head,int x){
    struct node* store=create_node(x);
    if(*head==NULL){
        *head =store;
        return;
    }
    struct node* temp=*head;
    while(temp->next){
        temp=temp->next;
    }
    temp->next=store;
}

//Reverse the linked list
struct node* reverse(node* head, int k){
	if(head==NULL){
		return NULL;
	}
	struct node* next=NULL;
	struct node* prev=NULL;
	struct node* curr=head;
	int count=0;
	while(curr && count<k){
		//we store the next pointer into next
		//and connect the  current pointer to the head of the prev
		next=curr->next;
		curr->next=prev;
		prev=curr;
		curr=next;
		count++;
	}
	//in the reverse list head node of the linkedlist will come to the tail
	//for this we connect the next node to it's next pointer 
	head->next=reverse(curr,k);
	return prev;
}

//Print the list
void print(node* head){
	struct node* temp=head;
	while(temp){
		cout<<temp->data<<" ";
		temp=temp->next;
	}
}

int main()
{
    struct node* l=NULL;
    push(&l,1);
    push(&l,2);
    push(&l,3);
    push(&l,4);
    push(&l,5);
    push(&l,6);
    cout<<"Before the reverse operation"<<endl;
    print(l);
    l=reverse(l,2);
    cout<<"\nAfter the reverse operation"<<endl;
    print(l);
    return 0;
}

Output

输出量

Before the reverse operation
1 2 3 4 5 6
After the reverse operation
2 1 4 3 6 5


翻译自: https://www.includehelp.com/cpp-programs/reverse-a-linked-list-in-groups-of-given-size-using-cpp-program.aspx

给定长度为n+1的数组

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值