1. Transpose the linked list in θ (n) time and only require a small amount of extra space
Here you need 3 pointers so that Q points to p and then moves back in turn.
Code:
#include <iostream>
using namespace Std;
typedef struct NODE
{
int data;
struct Node *next;
}linklist;
linklist* Create_end ();
void Printlinklist (linklist *l);
linklist* create_end ()
{
Linklist *head, *e, *p;
Head=new linklist;
int key;
e = head;
printf ("Please enter the data you want to insert, #结束! \ n ");
while (scanf ("%d", &key) ==1)
{
p = new Linklist;
P->data = key;
E->next = p;
e = p;
}
E->next = NULL;
return head;
}
void Printlinklist (linklist *l)
{
linklist *p = l->next;
while (p->next! = NULL)
{
printf ("%d->", p->data);
p = p->next;
}
printf ("%d", p->data);
printf ("\ n");
}
linklist* Reverse (linklist *l)
{
linklist* P,*q,*r;
p=l->next;
q=p->next;
r=q->next;
p->next=null;
while (R->next!=null)
{
q->next=p;
p=q;
Q=r;
r=r->next;
}
q->next=p;
r->next=q;
l->next=r;
return L;
}
int main (void)
{
Linklist *linklist1=new linklist;
Linklist1=create_end ();
Printlinklist (LINKLIST1);
printf ("The converted linked list is:");
Linklist1=reverse (LINKLIST1);
Printlinklist (LINKLIST1);
}
"Algorithm design-reversal of single chain list" single-linked list reversal implementation