2.7链表 回文链表
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <queue> 5 #include <algorithm> 6 using namespace std; 7 8 struct ListNode 9 { 10 int val; 11 struct ListNode *next; 12 ListNode(int x):val(x), next(NULL) {} 13 }; 14 15 ListNode* create(vector<int> v) 16 { 17 ListNode *p = new ListNode(0); 18 ListNode *temp1 = p; 19 for (int i = 0;i < v.size();i++) 20 { 21 ListNode *temp2 = new ListNode(v[i]); 22 temp1->next = temp2; 23 temp1 = temp1->next; 24 } 25 return p->next; 26 } 27 28 class Palindrome { 29 public: 30 bool isPalindrome(ListNode* pHead) { 31 // write code here 32 vector<int> v1; 33 vector<int> v2; 34 while (pHead) 35 { 36 v1.push_back(pHead->val); 37 pHead = pHead->next; 38 } 39 v2 = v1; 40 reverse(v2.begin(), v2.end()); 41 return v1 == v2; 42 } 43 }; 44 45 int main() 46 { 47 vector<int> v{ 1,2,3,2,1}; 48 ListNode* p = create(v); 49 Palindrome solution; 50 cout<<solution.isPalindrome(p); 51 return 0; 52 }
-------------------------------------------------
原创博客 转载请注明出处http://www.cnblogs.com/hslzju
-------------------------------------------------