"Algorithmic problems" using recursion and non-recursion to implement the transpose of a unidirectional list

Source: Internet
Author: User

In the process of reading any questions, welcome to communicate together

Email: [email protected]    

QQ:1494713801

Problem:

Give a one-way list and reverse it from beginning to end. For example, C->d, a, b , A, B, C , a .

Analysis:

Suppose that the structure of each node is:

Class Node {char value; Node Next;}

The non-recursive code is as follows:

1. void reverse (struct Node **list)

2. {

3. struct Node *currentp = *list;

4. struct Node *pleft = NULL;

5. struct Node *pright = NULL;

6.

7.

8. while (CURRENTP! = NULL) {

9. Pright = currentp->next;

Ten. Currentp->next = Pleft;

Pleft = CURRENTP;

CURRENTP = Pright;

13.}

*list = Pleft;

15.}


The recursive way code is as follows:

1. struct node* recursive_reverse (struct Node *list)

2. {

3. struct Node *head = list;

4. struct Node *p = r_reverse (list);

5. Head->next = NULL;

6. return p;

7.}

8.

9. struct node *r_reverse (struct node *list)

10. {

One. if (NULL = = List | | NULL = = List->next)

A. return list;

struct Node *p = R_reverse (List->next);

List->next->next = list;

. return p;

16.}

The recursive method is actually very clever, it uses recursion to go to the end of the list, and then update each node's next value (the second sentence of the code). In the above code, the value of reverserest does not change for the last node of the linked list, so, after inversion, we can get the new linked list head.

Single linked list adjacent elements transpose (non-recursive)

1. struct node* recursive_reverse (struct Node *list)

2. {

3. struct Node *head = list;

4. struct Node *p = r_reverse (list);

5. Head->next = NULL;

6. return p;

7.}

8.

9. struct node *r_reverse (struct node *list)

10. {

One. if (NULL = = List | | NULL = = List->next)

A. return list;

struct Node *p = R_reverse (List->next);

List->next->next = list;

. return p;

16.}


4 single-linked list adjacent elements transpose (recursive)

1. struct node * recursive_partial_reverse (struct node *list)

2. {

3. if (NULL = = List | | NULL = = List->next)

4. return list;

5. struct Node *p = list->next;

6. struct Node *node = Recursive_partial_reverse (List->next->next);

7. List->next->next = list;

8. list->next = node;

9. return p;

10.}

Reference Links:

http://blog.csdn.net/skylinesky/article/details/760694

"Algorithmic problems" using recursion and non-recursion to implement the transpose of a unidirectional list

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.