e.g.
input: 1 -> 2 -> 3 -> 4 -> 5
output: 5 -> 4 -> 3 -> 2 -> 1
Method 1: iterative version
令一個空的head pointer叫做newHead, 將原linked list從頭掃到尾一次, 依序將掃到的結點加入newHead指向的linked list, 其中必須用一個temp pointer紀錄下一個要加入的結點, 且將原head前進
- temp = OldHead
- Advance OldHead
- Add temp to newHead list: temp->next = newHead
- Update newHead: newHead = temp
- repeat 1.~4. until OldHead pointer to NULL pointer
- return newHead
Method 2: Recursive version
- Where is the sub-problem?
Sub-problem: 2 -> 3 -> 4 -> 5 -> NULL
Idea 1
1. Recursively reverse the sub-problem
2. the sub-problem part will return a pointer R points to the end of reversed linked list
3. Make R point the previous node P ( need another pointer P to keep tracking the previous node)
4. return P as the recursive part's result
5. Base case: current pointer points to NULL, change the head pointer to P
缺點:
1. 需要兩個參數,一個記錄當前子問題的頭,一個紀錄前一個Node
2. head之更改必須寫死在Base case中
3. 必須回傳reverse結果的尾結點
Idea 2
1. Recursively reverse the sub-problem
2. Base case: current node's next node is NULL, change the head pointer to current node
3. current node's next' next pointer point to current node
4. current node's next point to NULL
缺點:
1. head之更改必須寫死在Base case中
C++ code:
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
| #include <iostream>
using namespace std;
class Node{
public:
Node( ) : value(0), next(NULL){}
Node( int theValue, Node* nextNode ) : value(theValue), next(nextNode){}
int value;
Node* next;
};
class LinkedList{
public:
LinkedList( );
~LinkedList( );
void AddToEnd( int theValue );
void AddToFront( int theValue );
void DeleteFront( );
void ShowElements( ) const;
void Reverse( ); // iterative
void RecursiveReverseComplex( ); // two parameter
void RecursiveReverseSimple( ); // one parameter
private:
Node* head;
Node* recurReverseComplex( Node*, Node* ); // for RecursiveReverseComplex
void recurReverseSimple( Node* ); // for RecursiveReverseSimple
};
LinkedList::LinkedList( ): head(NULL){ }
LinkedList::~LinkedList( ){
while( head != NULL )
DeleteFront();
}
void LinkedList::AddToEnd( int theValue ){
Node *current = head;
// empty List
if( current == NULL ){
AddToFront( theValue );
return;
}
// Non-empty list
while( current->next != NULL )
current = current->next;
Node *newNode = new Node(theValue, NULL);
current->next = newNode;
}
void LinkedList::AddToFront( int theValue ){
Node *newNode = new Node(theValue, head);
head = newNode;
}
void LinkedList::DeleteFront( ){
if( head == NULL ) return;
Node *headNext = head->next;
delete head;
head = headNext;
}
void LinkedList::ShowElements( ) const{
Node *current = head;
while( current != NULL ){
cout << current->value << " ";
current = current->next;
}
cout << endl;
}
void LinkedList::Reverse( ){
if( head == NULL ) return;
Node *current = head;
Node *newHead = NULL;
while( current != NULL ){
Node *temp = current;
current = current->next;
temp->next = newHead;
newHead = temp;
}
head = newHead;
}
void LinkedList::RecursiveReverseComplex( ){
recurReverseComplex(head, NULL);
}
Node* LinkedList::recurReverseComplex( Node* current, Node* previous){
if( current == NULL ){
head = previous;
return head;
}
Node *reversedEnd = recurReverseComplex(current->next, current);
reversedEnd->next = previous;
return previous;
}
void LinkedList::RecursiveReverseSimple( ){
recurReverseSimple( head );
}
void LinkedList::recurReverseSimple( Node* current ){
if( current->next == NULL )
{
head = current;
return;
}
recurReverseSimple( current->next );
current->next->next = current;
current->next = NULL;
}
int main( ){
LinkedList list;
list.AddToEnd( 2 );
list.AddToEnd( 3 );
list.AddToEnd( 4 );
list.AddToEnd( 5 );
list.AddToFront( 1 );
list.AddToFront( 0 );
list.ShowElements( );
list.DeleteFront( );
list.ShowElements( );
list.Reverse( );
list.ShowElements( );
list.RecursiveReverseComplex( );
list.ShowElements( );
list.RecursiveReverseSimple( );
list.ShowElements( );
return 0;
}
|
沒有留言:
張貼留言