-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24_Reverse_List.cpp
More file actions
118 lines (90 loc) · 2.13 KB
/
24_Reverse_List.cpp
File metadata and controls
118 lines (90 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// Created by mark on 2019/7/8.
// Copyright © 2019年 mark. All rights reserved.
//
/*
说明:
1. 问题:24.链表反转
2. 思路:主要是防止断链,定义前中后三个节点,每次后移
*/
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
using namespace std;
struct ListNode
{
int val;
ListNode* next;
}LN,*pLN;
/*
主要的测试问题:
1. 链表头指针是nullptr
2. 输入链表只有一个节点
3. 正常链表
*/
// 反转链表
ListNode* ReverseList(ListNode* pHead)
{
if(pHead == nullptr)
return nullptr;
ListNode* NewHead = nullptr;
ListNode* pre = nullptr;
ListNode* cur = pHead;
while(cur != nullptr)
{
ListNode* next = cur->next;
if(next == nullptr) // 如果链表只有一个头结点
NewHead = cur;
cur->next = pre;
pre = cur;
cur = next;
}
return NewHead;
}
// ------------------------------------------------------------------------------------------------------------------------
// 辅助函数,创建链表
ListNode* CreatListNode(int val)
{
ListNode* node = new ListNode();
node->val = val;
node->next = nullptr;
return node;
}
// 辅助函数,连接两个链表节点
void ConnectListNodes(ListNode* pre, ListNode* cur)
{
pre->next = cur;
}
// 遍历链表
void PrintList(ListNode* node)
{
while(node != nullptr)
{
cout << node->val << " ";
node = node->next;
}
}
int main(){
ListNode* p1 = CreatListNode(1);
ListNode* p2 = CreatListNode(2);
ListNode* p3 = CreatListNode(3);
ListNode* p4 = CreatListNode(4);
ListNode* p5 = CreatListNode(5);
ListNode* p6 = CreatListNode(6);
ConnectListNodes(p1, p2);
ConnectListNodes(p2, p3);
ConnectListNodes(p3, p4);
ConnectListNodes(p4, p5);
ConnectListNodes(p5, p6);
cout << "原链表是:";
PrintList(p1);
cout << endl;
cout << "反转后是:";
ListNode* p = ReverseList(p1);
PrintList(p);
cout << endl;
return 0;
}