[] Painted cartoon illustrates single chain reversal / reverse single linked list (data structure)

Algorithms and data structures illustrated

1, EDITORIAL

This is a classic problem, [singly linked list in reverse order] problems.

Many companies have this question in the interview exam, some companies clearly can not be used to subject to additional nodes of storage space, and some did not specify, but if the interviewer used the extra storage space in a transit node, it will be a relatively low scores.
Here Insert Picture Description
So how, without the use of additional storage nodes so that a single list of all nodes in reverse order?

After a thousand people there are a thousand Hamlet, then I did not understand ,,, and finally manually push the code again, just probably understand this process, hand-drawn comic illustration look here! ! !

As used herein, is thought iteration of the loop, to analyze the problem.


2, the code

typedef int ElementType;
typedef struct LNode *PtrToLNode;//单链表定义
struct LNode{
    ElementType Data;
    PtrToNode   Next;
};
typedef PtrToLNode List;

List Reverse(List L){
	//将单链表L逆转
	PtrToLNode new_head, old_head, temp;
	
	old_head=L;			//初始化当前旧表头为L
	new_head=NULL;		//初始化逆转后新表头为空

	while(old_head){	//当旧表头不为空时
		temp=old_head->Next;
		old_head->Next=new_head;
		new_head=old_head;
		old_head=temp;
	}
	L=new_head;			//更新L
	return L;
}

[Analysis]: Here the idea is to solve this problem: the use of the cycle , starting from the head of the list one by one process. Cycle design, the most central point is how to grasp the loop invariant . Loop invariant shows a change in the nature of the cycle process, without depending on the number of repetitions assertion process previously performed.

Loop invariant body is invariant, which is a kind of regular expression description. The process is divided into three parts: an initial, retention, terminated.
(1) Initial: to ensure that the invariant is true at the initial time.
(2) to keep: to ensure that are true in the beginning and end of each cycle of the same type.
(3) Termination: If the program can be terminated under certain conditions, so at the time of termination, you can get the right results you want.
----Baidu Encyclopedia

For this question, the round before the start of the cycle, are faced with two lists, which old_headis a list to be reversed (ie, the "old" head of the list), and new_headis a good list has been reversed (ie, the "new" head of the list ). After each cycle perform well, old_headand new_headstill point to a new list to be reversed and reversed already good list.


3, the body

  1. The preceding first program, a definition of the way a single list.
  2. Before running function,
    1. Create a pointer new_head, pointing to the content is empty NULL( );
    2. Create a pointer old_head, pointing to the content list of the first node ( L);
    3. Create a pointer temp;
    4. Suppose, Reverse( List L );the function list is received, the contents of A, B, C, D (for ease of presentation);
  3. The above process as shown below:

Here Insert Picture Description
4. After the initialization state as follows:

Here Insert Picture Description
5 temp = old_head->Next;represents the old_head->Nextpoint temp, is Bthe following temp;

Here Insert Picture Description
6 old_head->Next = new_head;represents the old_head->Nextpoint new_head, put old_head->Nextand Bdirectly off point new_head;

Here Insert Picture Description
7. On completion of the step, this step is much simpler, i.e., the position of each pointer pointing to modify, to continue to facilitate the next step Bis connected to Athe back, as shown below:

Here Insert Picture Description
8 new_head=old_head;, and old_head=temp;then temp=old_head->Next;(a new cycle), as the new position after the movement;
9. Repeat the above process cycle, until old_headas NULL;
10. Finally execution L=new_head;, update L, ending function.


4. Examples

A question from Zhejiang University data structure, made some adaptations:
Here Insert Picture Description

#include <stdio.h>
#include <stdlib.h>
 
typedef int ElementType;
typedef struct LNode *PtrToLNode;
struct LNode{
    ElementType Data;
    PtrToLNode   Next;
};
typedef PtrToLNode List;
 
List Read(); 			/* 细节在此不表 */
void Print( List L ); 	/* 细节在此不表 */
List Reverse( List L );
 
int main(){
    List L1, L2;
    L1 = Read();
    L2 = Reverse(L1);
    Print(L2);
    return 0;
}
 
/* 你的代码将被嵌在这里 */

Sample input:

5
1 3 4 5 2

Sample output:

2 5 4 3 1


Complete code:

#include<stdio.h>
#include<stdlib.h>

typedef int ElementType;
typedef struct LNode *PtrToLNode;
struct LNode{
    ElementType Data;
    PtrToLNode  Next;
};
typedef PtrToLNode List;

List Read();
void Print(List L);
List Reverse(List L);

int main(){
    List L1, L2;
    L1 = Read();
    L2 = Reverse(L1);
    Print(L2);
  
    return 0;
}

/*建立链表*/
List Read(){
	List current;
	List head=NULL;
	List prev=NULL;
	int len=0;
	scanf("%d",&len);
	while(len--){
		current=(List)malloc(sizeof(struct LNode));
		if(head==NULL)
			head=current;
		else
			prev->Next=current;
		
		current->Next=NULL;
		scanf("%d",&current->Data);
		prev=current;
	}
	return head;
}

/*输出链表*/
void Print(List L){
	List p=L;
	if(p==NULL)
		printf("NULL\n");
	else
		printf("\n");
	while(p!=NULL){
		printf("%d ",p->Data);
		p=p->Next;
	}
}

/*单链表逆序*/
//代码来自,数据结构第二版(浙江大学),陈越等
List Reverse(List L){
	PtrToLNode new_head, old_head, temp;
	
	old_head=L;
	new_head=NULL;

	while(old_head){
		temp=old_head->Next;
		old_head->Next=new_head;
		new_head=old_head;
		old_head=temp;
	}
	L=new_head;
	return L;
}

Running through the sample.
Here Insert Picture Description

reference

  • Data structure of the second edition (Zhejiang University), Chen Yue, etc.
  • https://blog.csdn.net/Mas1461261388/article/details/80097158
Published 232 original articles · won praise 4989 · Views 790,000 +

Guess you like

Origin blog.csdn.net/TeFuirnever/article/details/105157764