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;
}