ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2.JZ24 反转链表

2022-08-18 13:00:09  阅读:143  来源: 互联网

标签:Node head new1 cur 反转 JZ24 next 链表 NULL


C++

 1 #include "stdafx.h"
 2 #include <stdlib.h>
 3 struct Node{
 4     int data;
 5     Node* next;
 6 };
 7 
 8 void print1(Node *head)  
 9 {  
10     Node *p;  
11     p=head;  
12     if(head!= NULL)  
13     do  
14     {  
15     printf("%d \n", p->data);  
16     p=p->next;  
17     }while(p!=NULL);  
18 }
19 
20 Node* ReverseList(Node* head)
21 {
22     if(head==NULL)
23         return NULL;
24 
25     Node* cur=head;
26     Node* pre=NULL;
27     Node* nx=NULL;
28     while(cur->next!=NULL)
29     {
30         nx=cur->next;
31         cur->next=pre;
32         pre=cur;
33         cur=nx;
34     }
35     cur->next=pre;
36     return cur;
37 }
38 Node* init( int num) // insert from back  
39 {  
40     if(0 >= num)  
41         return NULL;  
42     Node* cur, pre;  
43     Node* head = NULL;  
44     int i = 0; cur = head;  
45     Node* new1 = (Node*)malloc(sizeof(Node));  
46     new1->data = 1;  
47     head = cur = new1;  
48     for(i = 1; i < num; i++)  
49     {  
50         Node* new1=(Node*)malloc(sizeof(Node));  
51         new1->data = i + 1;  
52         cur->next = new1;  
53         cur = new1;  
54     }  
55      cur->next = NULL;     
56     return head;      
57 }  
58 int _tmain(int argc, _TCHAR* argv[])
59 {
60     Node* list =NULL;
61     list=init(10);
62     print1(list);
63     Node* newlist=ReverseList(list);
64     print1(newlist);
65     getchar();
66     return 0;
67 }

 

标签:Node,head,new1,cur,反转,JZ24,next,链表,NULL
来源: https://www.cnblogs.com/sundayvc/p/16598319.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有