《Cracking the Coding Interview》——第2章:链表——题目7

2014-03-18 02:57

题目:检查链表是否是回文的,即是否中心对称。

解法:我的做法是将链表从中间对半拆成两条,然后把后半条反转,再与前半条对比。对比完了再将后半条反转了拼回去。这样不涉及额外的空间,比反转整条链表然后比较要来的好。如果你反转了整条链表又不用额外空间,接下来跟谁比去呢?

代码:

  1 // 2.7 To Check the given linked list is palindrome or not?

  2 #include <cstdio>

  3 #include <unordered_set>

  4 using namespace std;

  5 

  6 struct ListNode {

  7     int val;

  8     struct ListNode *next;

  9     ListNode(int x): val(x), next(nullptr) {};

 10 };

 11 

 12 class Solution {

 13 public:

 14     bool isPalindromeList(ListNode *head) {

 15         if (head == nullptr) {

 16             return false;

 17         }

 18         if (head->next == nullptr) {

 19             return true;

 20         }

 21         

 22         ListNode *t1, *run, *h2;

 23         

 24         t1 = run = head;

 25         while (run->next != nullptr && run->next->next != nullptr) {

 26             t1 = t1->next;

 27             run = run->next->next;

 28         }

 29         h2 = t1->next;

 30         t1->next = nullptr;

 31         h2 = reverseList(h2);

 32         

 33         ListNode *p1, *p2;

 34         p1 = head;

 35         p2 = h2;

 36         while (p2 != nullptr) {

 37             if (p1->val != p2->val) {

 38                 break;

 39             } else {

 40                 p1 = p1->next;

 41                 p2 = p2->next;

 42             }

 43         }

 44         bool res = (p2 == nullptr);

 45         

 46         h2 = reverseList(h2);

 47         t1->next = h2;

 48         

 49         return res;

 50     }

 51 private:

 52     ListNode* reverseList(ListNode* head) {

 53         if (head == nullptr) {

 54             return head;

 55         }

 56         

 57         ListNode* new_head = nullptr;

 58         ListNode* ptr;

 59         

 60         while (head != nullptr) {

 61             if (new_head == nullptr) {

 62                 new_head = head;

 63                 head = head->next;

 64                 new_head->next = nullptr;

 65             } else {

 66                 ptr = head->next;

 67                 head->next = new_head;

 68                 new_head = head;

 69                 head = ptr;

 70             }

 71         }

 72         

 73         return new_head;

 74     }

 75 };

 76 

 77 int main()

 78 {

 79     int i;

 80     int n;

 81     int val;

 82     struct ListNode *head, *ptr;

 83     Solution sol;

 84     

 85     while (scanf("%d", &n) == 1 && n > 0) {

 86         // create a linked list

 87         ptr = head = nullptr;

 88         for (i = 0; i < n; ++i) {

 89             scanf("%d", &val);

 90             if (head == nullptr) {

 91                 head = ptr = new ListNode(val);

 92             } else {

 93                 ptr->next = new ListNode(val);

 94                 ptr = ptr->next;

 95             }

 96         }

 97         

 98         // check if the list is a palindrome

 99         if (sol.isPalindromeList(head)) {

100             printf("It's a palindrome.\n");

101         } else {

102             printf("It's not a palindrome.\n");

103         }

104         

105         // print the list

106         printf("%d", head->val);

107         ptr = head->next;

108         while (ptr != nullptr) {

109             printf("->%d", ptr->val);

110             ptr = ptr->next;

111         }

112         printf("\n");

113         

114         // delete the list

115         while (head != nullptr) {

116             ptr = head->next;

117             delete head;

118             head = ptr;

119         }

120     }

121     

122     return 0;

123 }

 

你可能感兴趣的:(interview)