Single linked list inversion C language implementation __c language

Source: Internet
Author: User

A single linked list can be reversed by using a loop, or by using a recursive method

1. Cycle reversal single linked list

In the loop method, use the pre to point to the previous node, Cur point to the current node, each time pointing the Cur->next to the pre.


Code:

# include <iostream>
# include <cstdlib>
using namespace std;

struct Linknode
{
int val;
Linknode *next;
Linknode (int x): Val (x), Next (NULL) {}
};
Linknode *reverse2 (Linknode *head)
{
	if (head==null) return
		NULL;
	Linknode *pre=null;
	Linknode *p=head;
	Linknode *h=null;
	while (p)
	{
		h=p;
		Linknode *tmp=p->next;
		p->next=pre;
		pre=p;
		p=tmp;
	}
	return h;         Return header node
int main ()                //test code
{
	Linknode *head=new linknode (1);
	Linknode *p1=new Linknode (2);
	Linknode *p2=new Linknode (3);
	head->next=p1;
	p1->next=p2;      Establish the chain list   1->2->3->null
	linknode *p=reverse2 (head);
	while (p)
	{
		cout<<p->val<<endl;
		p=p->next;
	}                Output is  3->2->1->null
system ("pause");
return 0;
}
2. Recursive implementation of single linked list inversion

# include <iostream>
# include <cstdlib>
using namespace std;

struct Linknode
{
int val;
Linknode *next;
Linknode (int x): Val (x), Next (NULL) {}
};

Linknode *reverse (Linknode *head,linknode * &newhead)   //head is the head node of the original linked list, Newhead is the head node of the new list
{ 
if (head== NULL) return
	null;
if (head->next==null)
{
		newhead=head;
}
else
{
	reverse (head->next,newhead);
	head->next->next=head;
	head->next=null;
}
return newhead;
}


int main ()                                  //test code
{
	Linknode *head=new linknode (1);
	Linknode *p1=new Linknode (2);
	Linknode *p2=new Linknode (3);
	head->next=p1;
	p1->next=p2;                            Set up a list 1->2->3->null;
	Linknode *newhead=null;
	Linknode *p=reverse (head,newhead);      
	while (p)
	{
		cout<<p->val<<endl;
		p=p->next;
	}                                   Output Chain list 3->2->1->null;
System ("pause");
return 0;
}

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.