"Algorithm design-reversal of single chain list" single-linked list reversal implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.