1、链表翻转。给出一个链表和一个数k,比如,链表为1→2→3→4→5→6,k=2,则翻转后2→1→6→5→4→3,若k=3,翻转后3→2→1→6→5→4,若k=4,翻转后4→3→2→1→6→5,用程序实现。
#include<iostream>
using namespace std;
struct List
{
int num;
List* next;
};
//创建链表
List* create_list(int n)
{
List* result = new List;
List* head = result;
cout << "接下来输入n个数:" << endl;
for (int i = 0; i < n; i++)
{
List* temp = new List;
int a;
cin >> a;
temp->num = a;
temp->next = NULL;
head->next= temp;
head = head->next;
}
return result;
}
//打印链表
void print_list(List* list1)
{
List* p = list1->next;
while (p->next)
{
cout << p->num << "->";
p = p->next;
}
cout << p->num<<endl;
}
//实现链表反转
List* ReverseList(List* list,int k)
{
List* pre,* cur,* nex,*end1;
List* start = list->next;