Chain table flip

Source: Internet
Author: User

Chain table flip

It is required that the chain table be flipped as quickly as possible.

We need to use two pointers, one pointing to the current node and the other pointing to the previous node of the current node. Each time we make the current node refer to the previous node to flip between the two nodes, and then move to realize the loop.

The Code is also very simple, as shown below:

#include <iostream>using namespace std;struct Node{int key;Node* next;};Node* createList(int arr[],int nLength);Node* reverseList(Node* head);void printList(Node* head);void clearList(Node* head);void main(){int arr[] = {1,3,5,7,9};int nLength = sizeof(arr)/sizeof(arr[0]);Node* head = createList(arr,nLength);printList(head);head = reverseList(head);printList(head);clearList(head);}Node* createList(int arr[],int nLength){Node* head = new Node;head->key = arr[0];head->next = NULL;Node *p = head;for(int i=1;i<nLength;i++){Node* ptr = new Node;ptr->key = arr[i];ptr->next = NULL;p->next = ptr;p = p->next;}return head;}Node* reverseList(Node* head){Node* preNode = NULL;Node* pNode = head;while( pNode != NULL ){Node* pNext = pNode->next;pNode->next = preNode;preNode = pNode;pNode = pNext;}return preNode;}void printList(Node* head){Node* p = head;while( p!= NULL ){cout<<p->key<<endl;p=p->next;}}void clearList(Node* head){Node* p = head;Node* ptr;while( p!= NULL ){ptr = p->next;delete p;p = ptr;}}



How to reverse a linked list

Chain table reversal

The reversal of a one-way linked list is a frequently asked interview question, which is also a very basic question. For example, a linked list is like this: 1-> 2-> 3-> 4-> 5, and then 5-> 4-> 3-> 2-> 1. The easiest way to traverse the linked list is to use an auxiliary pointer to store the next element pointed to by the current pointer during the traversal process. After the pointer of the element on the current node is reversed, use the stored pointer to traverse the backend. The source code is as follows:

Struct linka {
Int data;
Linka * next;
};

Void reverse (linka * & head)
{
If (head = NULL)
Return;
Linka * pre, * cur, * ne;
Pre = head;
Cur = head-> next;
While (cur)
{
Ne = cur-> next;
Cur-> next = pre;
Pre = cur;
Cur = ne;
}
Head-> next = NULL;
Head = pre;
}

There is also a method that uses recursion. The basic idea of this method is to call recursive functions to reverse the subsequent nodes before reversing the current node. The source code is as follows. However, this method has one disadvantage: The last node after the inversion will form a ring, so the next field of the node returned by the function must be set to NULL. I used a reference to change the head pointer. The source code of the algorithm is as follows:

Linka * reverse (linka * p, linka * & head)
{
If (p = NULL | p-> next = NULL)
{
Head = p;
Return p;
}
Else
{
Linka * tmp = reverse (p-> next, head );
Tmp-> next = p;
Return p;
}
}

Reverse of a single-chain table

The main problem with the original algorithm is that the first element (Header element) of the designed linked list does not contain valid data. Therefore, when reversing, it should start with the second element, at last, let the Header element point to the element after the reversal.

As follows:
List * turn (list * l ){
List * q, * r, * p, * s, * h;
P = l-> next;
If (p = NULL)
Return l;

Q = p-> next;
P-> next = NULL;
While (q! = NULL ){
R = q-> next;
Q-> next = p;
P = q;
Q = r ;}
L-> next = p;
H = l;
Return h;
}

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.