-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathisPalindrome2.h
103 lines (92 loc) · 3.01 KB
/
isPalindrome2.h
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
//
// isPalindrome2.h
// linkedList
//
// Created by junl on 2019/11/5.
// Copyright © 2019 junl. All rights reserved.
//
#ifndef isPalindrome2_hpp
#define isPalindrome2_hpp
#include <stdio.h>
#include <stack>
#include "creatlist.h"
/*
给定一个链表的头节点,请判断该链表是否为回文结构
进阶:
如果链表长度为N,要求时间复杂度为O(N),空间复杂度为O(1)
*/
namespace itinterviews {
//bad
class isPalindrome1{
public:
//思路:利用栈的结构,链表依次进栈,那么出栈的顺序正好是链表从右到左的顺序,如果和链表从左到右的顺序一直的话,那么就是回文的.
//时间复杂度为O(N),空间复杂度O(N)
bool solve(ListNode *head){
if (!head || !head->next)
return true;
ListNode *node = head;
std::stack<ListNode *> stk;
while (node) {
stk.push(node);
node = node->next;
}
node = head;
while (!stk.empty()) {
if (stk.top()->val != node->val) {
return false;
}
stk.pop();
node = node->next;
}
return true;
}
};
//good
class isPalindrome2{
public:
//思路:反转后半部分的链表,然后一一比较
//O(N), O(1)
bool solve(ListNode *head){
if (!head || !head->next)
return true;
ListNode *fast,*slow;
fast = slow = head;
while (fast && fast->next) {
fast = fast->next->next;
slow = slow->next;
}
//slow就是后半截的头节点
ListNode *pre,*ct,*next;
pre = slow;
ct = pre->next;
pre->next = nullptr;
while (ct) {
next = ct->next;
ct->next = pre;
pre = ct;
ct = next;
}
//一一标记,pre为后半截的头节点和head比较
while (pre && head) {
if (pre->val != head->val) {
return false;
}
pre = pre->next;
head = head->next;
}
return true;
}
};
void test_isPalindrome2(){
std::cout << "-----判断该链表是否为回文结构-------" << std::endl;
ListNode *head1 = codinginterviews::creatLists({1,2,1})->next;
ListNode *head2 = codinginterviews::creatLists({1,2,2,2,1})->next;
ListNode *head3 = codinginterviews::creatLists({15,6,15})->next;
ListNode *head4 = codinginterviews::creatLists({1,2,3})->next;
// class isPalindrome2 so;
class isPalindrome2 so;
std::cout << so.solve(head1) << ", " << so.solve(head2) << ", "
<< so.solve(head3) << ", " << so.solve(head4) << std::endl;
}
}
#endif /* isPalindrome2_hpp */