PAT 浙大数据结构(Reversing Linked List)

本文介绍如何通过给定的常数K和单链表LL,实现对链表中每K个元素进行反转操作。详细阐述了输入输出规范,并提供了一个具体的代码实现示例,演示了如何遍历链表并按要求反转子链表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

按要求反转单链表

Given a constant KKK and a singly linked list LLL, you are supposed to reverse the links of every KKK elements on LLL. For example, given LLL being 1→2→3→4→5→6, if K=3K = 3K=3, then you must output 3→2→1→6→5→4; if K=4K = 4K=4, you must output 4→3→2→1→5→6. Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, a positive NNN (≤105\le 10^5≤10​5​​) which is the total number of nodes, and a positive KKK (≤N\le N≤N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

Then NNN lines follow, each describes a node in the format:

Address Data Next

where Address is the position of the node, Data is an integer, and Next is the position of the next node.
Output Specification:

For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input:

00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

Sample Output:

00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

反转一次

下面的代码中,first表示的是new即反转结点的前一个结点指针,second表示的是old即正在反转的结点指针,third表示的是tmp即反转结点的下一个结点指针.

下面是具体思路过程.
这里写图片描述步骤1
步骤2

代码实现:

#include <iostream>
#include <cstring>
#define Max 100000
using namespace std;
struct Node{
    char address[6];
   long order;
    char address_[6];
    Node *next;
};
Node* InitLink();
long Size(Node *head);
Node* Transfer(Node *head,long circle,long length)
{
   long n,i,cnt;
   n=length/circle;
   Node *first,*second,*third,*p,*s;
   p=head;
   first=head->next;
   second=first->next;

   for(i=0;i<n;i++)
  {
       cnt=1;
       while(cnt<circle)
    {
       third=second->next;
      // strcpy(third->address_,second->address)
       second->next=first;
       first=second;
       second=third;
       cnt++;

   }
    if(i==n-1) {
        p->next->next=second;
        p->next=first;
        break;
    }

        s=p->next;
        p->next->next=second;
        p->next=first;
        p=s;
        first=second;
       second=second->next;
  }
  return head;


}

void ChangeAddress_(Node *head)
{
     Node *p=head->next,*q=head->next;
     while(q->next)
     {    q=q->next;
     strcpy(p->address_,q->address);
          p=p->next;
     }

    strcpy(p->address_,"-1");
    return ;
}
void ShowLink(Node *head);
int main()
{
    Node *p[Max];
    Node *head,*q;
    head=InitLink();
  q=head;

    long amount,circle,i,length;

    char addr[6];
    cin>>addr>>amount>>circle;
    strcpy(q->address_,addr);
    for(i=0;i<amount;i++)

        {
            p[i]=new Node;
            cin>>p[i]->address>>p[i]->order>>p[i]->address_;
        }
    for(i=0;i<amount;)
    {
       if(strcmp(q->address_,p[i]->address)==0)
        { q->next=p[i];
          p[i]->next=NULL;
          q=p[i];
        i=0;
        }
        else i++;
        if(strcmp(q->address_,"-1")==0) break;
    }
    //cout<<"显示创建的链表:"<<endl;
   // ShowLink(head);
    length=Size(head);
    head=Transfer(head,circle,length);
    ChangeAddress_(head) ;
   // cout<<"翻转之后的链表:"<<endl;
    ShowLink(head);

    return 0;
}

Node* InitLink()
{
    Node *head;
    head=new Node;
    head->next=NULL;
    return head;
}
long Size(Node *head)
{
   long n=0;
  Node *p;
  p=head->next;
  while(p)
  {
      n++;
      p=p->next;
  }
  return n;
}
void ShowLink(Node *head)
{
    Node *p;
    p=head->next;
    while(p)
    {
       cout<<p->address<<" "<<p->order<<" "<<p->address_<<endl;
       p=p->next;
    }
    return ;
}

遗憾的是,由于水平有限,当N取到10^5时,程序超时,等待优化,也恳请各路大神指教.拜谢.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值