aboutsummaryrefslogtreecommitdiff
path: root/other/palindrome_linked_list.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'other/palindrome_linked_list.cpp')
-rw-r--r--other/palindrome_linked_list.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/other/palindrome_linked_list.cpp b/other/palindrome_linked_list.cpp
new file mode 100644
index 0000000..1b08a0c
--- /dev/null
+++ b/other/palindrome_linked_list.cpp
@@ -0,0 +1,32 @@
+#include "leetcode.h"
+
+class Solution {
+public:
+ bool isPalindrome(ListNode *head) {
+ ListNode *slow = head, *fast = head, *prev, *temp;
+ while (fast && fast->next) {
+ slow = slow->next;
+ fast = fast->next->next;
+ }
+ prev = slow;
+ slow = slow->next;
+ prev->next = NULL;
+ while (slow) {
+ temp = slow->next;
+ slow->next = prev;
+ prev = slow;
+ slow = temp
+ }
+ fast = head;
+ slow = prev;
+ while (slow) {
+ if (fast->val != slow->val)
+ return false;
+ else {
+ fast = fast->next;
+ slow = slow->next;
+ }
+ }
+ return true;
+ }
+};