A tentative approach to 001--single-link list inversion

Source: Internet
Author: User

The reversal problem of single-linked list is a very basic problem. The topics are as follows:
There is a single-linked list 1->2->3->4->5->6 inverted after the linked list is: 6->5->4->3->2->1.

Method One

Resolution: You can use the three pointer pre to temp,next the node by one. The detailed process is as follows:
(4) Initial state

pre = head;tmp = head->next;pre->nextnull;

(2) First cycle:

next = tmp->next;tmp->next = pre;

The pre and TMP move back one bit, the first loop ends, and the first node points to the head node.

pre = tmp;tmp = next;

(3) Second cycle

next = tmp->next;tmp->next = pre;

pre = tmp;tmp = next;

(4) This loop continues until the last node is reversed.

The specific code is as follows:

linklistReverselinklist (linklistHead) {if(Head = =NULL|| Head->Next==NULL)returnHead//Create three auxiliary pointer pre,tmp,NextlinklistPre,tmp,Next;//Initialize Pre, Tmppre = Head;tmp = head->Next;p re->Next=NULL;//Start traversal while(TMP! =NULL){Next= tmp->Next; Tmp->Next= Pre;    PRE = TMP; TMP =Next;! [Write a description of the picture here] (http://img.blog.csdn.net/20151016235255459)}head = pre;returnHead;}
Method Two

Parse: From the 2nd node to the nth node, after each node is inserted into the 1th node (head node), the first node is finally moved to the footer of the new table.
(1) Initial state

p = head->next;

(2) Start the cycle. The first loop inserts node 3 after Node 1 (steps in the code that correspond to each)

while(p->next){  q = p->next;  (1) p->next =q->next;  (2) q->next = head->next;  (3) head->next = q;}

(3) Add the first node to the tail of the list

    (1)p->next=head;//相当于成环     (2)head=p->next->next;//新head变为原head的next         p->next->next=NULL;//

The specific code is as follows:

Linklist reverselinklist (linklist head) {if(Head== NULL ||Head -Next== NULL)returnHead Linklist p,q;p=Head -Next//Start traversal while(p -Next) {Q=P -Next P -Next=Q -Next Q -Next=Head -Next Head -Next=Q;} P -Next=Head//equivalent to loopingHead=P -Next -Next//new head becomes the next of the original headP -Next -Next=NULL;//Break off ring    returnHead }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A tentative approach to 001--single-link list inversion

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.