String reverse
The last interview encountered a one-way list of the reverse sequence of the topic, fortunately the string reverse order more familiar with the analogy made. String reverse order is simpler, directly on the code:
void Stringreverse (char* p1,char* p2)
{
if (P1==P2) return;
Swap the value of P1, p2
*p1= (*P1) + (*P2);
*p2= (*P1)-(*P2);
*p1= (*P1)-(*P2);
if (p1==p2-1) return;
else Stringreverse (++p1,--p2);
}
Call Example:
Stringreverse (Str,str+strlen (str)-1);
Reverse order of one-way linked list
first three pictures: The original state is figure one, all the reverse order is the appearance of Tu San, figure II is a reverse step after the appearance. Follow the steps in Figure two to move one step at a time, and reverse the p1->next until all is reversed. Detailed principle see code.
Figure I:
Figure II:
Figure III:
typedef struct node node;
struct node{
int data;
Node* Next;
node* reverselinklist (node* head) {
if (head==null| | Head->next==null) {return head
;
}
node* P1 = head;
node* P2 = p1->next;
node* P3 = p2->next;
P1->next = Null;//p1 to end pointer, so point to NULL while
(p3!=null) {
p2->next = p1;//p2->next reverse
p1=p2;// P1 pointer moves backward p2=p3;//p2 pointer moves back one step
p3=p3->next;//p3 pointer moves backward
p2->next=p1;//p3 points to null, P2
the reverse head=p2;//header pointer to the previous tail pointer return head
;
}
It's over.