Inverted single linked list of C + + algorithm

Source: Internet
Author: User

Title: Define a function, enter the head node of a linked list, reverse the list and output the head node of the inverted list, the linked list node is defined as:

struct ListNode

int m_nvalue;

listnode* M_pnext;

};

Algorithm ideas:

Linked list 1-->2-->3-->4-->5

Establishes a Pprev node, and is an empty node; pprev = null; then establish a node Pnode = Phead; Then build a third node Pnext = pnode->m_pnext;

Pnext: Used to save a node behind a pnode, preventing the list from breaking in the middle, and then traversing Pprev and Pnode down:

Look at the code:

listnode* reverselist (listnode* head) {listnode* pnode = head; listnode* Pprev = Null;while (pnode! = NULL) {listnode* Pnext = pnode->m_pnext;//Save the value of the next node Pnode->m_pnext = pprev;// The next node of the current Pnode points to Pprevpprev = pnode;//at this point the pprev moves backwards to Pnodepnode = pnext;//at this point and pnode moves backwards, pointing to the pnext that was just saved;} return Pprev;//return Preversedhead;}


Full code:

ReverseList.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream>using namespace std;struct listnode{int m_nvalue; listnode* M_pnext; ListNode () {}listnode (int i): M_nvalue (i), M_pnext (NULL) {}};void addtotail (listnode* phead, int value) {listnode* pnew = New ListNode ();p new->m_nvalue = Value;pnew->m_pnext = null;if (Phead = = NULL) {phead = pnew;} else{listnode* Pnode = Phead;while (pnode->m_pnext!=null) {pnode = Pnode->m_pnext;} Pnode->m_pnext = Pnew;}} void Print (listnode* head) {listnode* Pnode = head;while (pnode!=null) {cout<<pnode->m_nvalue<<endl; Pnode = Pnode->m_pnext;}} listnode* reverselist (listnode* head) {listnode* pnode = head; listnode* Pprev = Null;while (pnode! = NULL) {listnode* Pnext = Pnode->m_pnext;pnode->m_pnext = PPrev;pPrev = PNode;p Node = Pnext;} return Pprev;//return Preversedhead;} int _tmain (int argc, _tchar* argv[]) {/*listnode* head = new ListNode (1); listnode* Node1 = new ListNode (2); listnode* Node2 = new ListnodE (3); listnode* node3 = new ListNode (4); listnode* node4 = new ListNode (5); head->m_pnext = Node1;node1->m_pnext = Node2;node2->m_pnext = Node3;node3-&gt ; m_pnext = Node4;node4->m_pnext = null;*/listnode* pNode1 = new ListNode (1);//print (PNODE1); Addtotail (pnode1,2); Addtotail (pnode1,3); Addtotail (pnode1,4); Addtotail (pnode1,5);cout<< "invert before:" <<endl; Print (PNODE1);//print (head); listnode* Pnode = reverselist (pNode1);cout<< "after reversal" <<endl; Print (Pnode); GetChar (); return 0;}


Code can run the test pass!

Inverted single linked list of C + + algorithm

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.