-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpalindrome2.cpp
131 lines (109 loc) · 2.92 KB
/
palindrome2.cpp
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
// {"category": "Algorithm", "notes": "Check if a linked list is palindrome"}
#include <SDKDDKVer.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
using namespace std;
//------------------------------------------------------------------------------
//
// Implement a function to check if a linked list is a palindrome.
//
//------------------------------------------------------------------------------
template<class Object>
class ListNode
{
public:
Object m_object;
ListNode<Object>* m_pNext;
};
//------------------------------------------------------------------------------
//
// Implementation
//
//------------------------------------------------------------------------------
template<class Object>
bool IsPalindrome(ListNode<Object>*& pHead, ListNode<Object>* pTail)
{
if (nullptr == pHead || nullptr == pTail)
{
return false;
}
if (pTail->m_pNext != nullptr)
{
if (!IsPalindrome(pHead, pTail->m_pNext))
{
return false;
}
}
bool result = (pHead->m_object == pTail->m_object);
pHead = pHead->m_pNext;
return result;
}
template<class Object>
bool IsPalindrome(ListNode<Object>* pRoot)
{
return IsPalindrome(pRoot, pRoot);
}
//------------------------------------------------------------------------------
//
// Demo execution
//
//------------------------------------------------------------------------------
void printResult(ListNode<char>* pNode)
{
cout << (IsPalindrome(pNode) ? "Yes : " : "No : ");
for (ListNode<char>* p = pNode; p != nullptr; p = p->m_pNext)
{
cout << p->m_object << (p->m_pNext ? " -> " : "");
}
cout << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
ListNode<char> node1, node2, node3, node4, node5;
node1.m_object = 'a';
node2.m_object = 'b';
node3.m_object = 'c';
node4.m_object = 'b';
node5.m_object = 'a';
node1.m_pNext = &node2;
node2.m_pNext = &node3;
node3.m_pNext = &node4;
node4.m_pNext = &node5;
node5.m_pNext = nullptr;
printResult(&node1);
node1.m_object = 'a';
node2.m_object = 'b';
node3.m_object = 'b';
node4.m_object = 'a';
node1.m_pNext = &node2;
node2.m_pNext = &node3;
node3.m_pNext = &node4;
node4.m_pNext = nullptr;
printResult(&node1);
node1.m_object = 'a';
node2.m_object = 'b';
node3.m_object = 'c';
node1.m_pNext = &node2;
node2.m_pNext = &node3;
node3.m_pNext = nullptr;
printResult(&node1);
node1.m_object = 'a';
node2.m_object = 'b';
node3.m_object = 'c';
node4.m_object = 'a';
node1.m_pNext = &node2;
node2.m_pNext = &node3;
node3.m_pNext = &node4;
node4.m_pNext = nullptr;
printResult(&node1);
node1.m_object = 'a';
node1.m_pNext = nullptr;
printResult(&node1);
node1.m_object = 'a';
node2.m_object = 'b';
node1.m_pNext = &node2;
node2.m_pNext = nullptr;
printResult(&node1);
return 0;
}