Write a recursive version of the linked list transpose Program

Source: Internet
Author: User

The transpose of the linked list is a very common and basic data structure problem. The non-recursive algorithm is very simple. You can use three temporary pointers to loop through the linked list. Recursive Algorithms are also relatively simple, but it is difficult to write them out at half past one if the idea is unclear. The following is a recursive version of the linked list transpose program:


[Cpp]
# Include <iostream>
Using namespace std;
 
Typedef struct Node
{
Node (int v, Node * ptr = NULL): data (v), next (ptr ){}
Int data;
Node * next;
} SNode;
 
// LB_c: Output linked list. head is the linked list header pointer.
Void outputList (sNode * head)
{
SNode * p = head;
While (p! = NULL)
{
Cout <p-> data <"-> ";
P = p-> next;
}
Cout <"NULL" <endl;
}
 
// LB_c: Recursive Implementation of transpose linked list
SNode * reverseList (sNode * head)
{
// LB_c: exception judgment (NULL = head) end condition (NULL = head-> nex ),
// When the head is the last node, return the node, that is, the head node of the transpose linked list.
If (NULL = head) | (NULL = head-> next ))
Return head;
// LB_c: recursive subsequent linked list (that is, the linked list with head-> next as the first node)
SNode * pNewHead = reverseList (head-> next );
// LB_c: after the previous step is completed, head-> next is the end node of the subsequent linked list,
// So let the head-> next point to the current node head
Head-> next = head;
// LB_c: the next of the current node points to NULL.
Head-> next = NULL;
// LB_c: returns the header pointer of the subsequent linked list.
Return pNewHead;
}
 
Int main ()
{
// LB_c: Create a linked list
SNode n5 (5 );
SNode n4 (4, & n5 );
SNode n3 (3, & n4 );
SNode n2 (2, & n3 );
SNode n1 (1, & n2 );
 
// LB_c: Output original linked list
OutputList (& n1 );
 
// LB_c: Call the transpose Function
SNode * pNew = reverseList (& n1 );
 
// LB_c: output the transposed linked list
OutputList (pNew );
 
Return 0;
}

# Include <iostream>
Using namespace std;

Typedef struct Node
{
Node (int v, Node * ptr = NULL): data (v), next (ptr ){}
Int data;
Node * next;
} SNode;

// LB_c: Output linked list. head is the linked list header pointer.
Void outputList (sNode * head)
{
SNode * p = head;
While (p! = NULL)
{
Cout <p-> data <"-> ";
P = p-> next;
}
Cout <"NULL" <endl;
}

// LB_c: Recursive Implementation of transpose linked list
SNode * reverseList (sNode * head)
{
// LB_c: exception judgment (NULL = head) end condition (NULL = head-> nex ),
// When the head is the last node, return the node, that is, the head node of the transpose linked list.
If (NULL = head) | (NULL = head-> next ))
Return head;
// LB_c: recursive subsequent linked list (that is, the linked list with head-> next as the first node)
SNode * pNewHead = reverseList (head-> next );
// LB_c: after the previous step is completed, head-> next is the end node of the subsequent linked list,
// So let the head-> next point to the current node head
Head-> next = head;
// LB_c: the next of the current node points to NULL.
Head-> next = NULL;
// LB_c: returns the header pointer of the subsequent linked list.
Return pNewHead;
}

Int main ()
{
// LB_c: Create a linked list
SNode n5 (5 );
SNode n4 (4, & n5 );
SNode n3 (3, & n4 );
SNode n2 (2, & n3 );
SNode n1 (1, & n2 );

// LB_c: Output original linked list
OutputList (& n1 );

// LB_c: Call the transpose Function
SNode * pNew = reverseList (& n1 );

// LB_c: output the transposed linked list
OutputList (pNew );

Return 0;
}


 

Related Article

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.