# Leetcode 234：回文链表

## 代码实现

bool isPalindrome(ListNode* head) { if (!head || !(head->next)) return true; ListNode* slow = head; ListNode* fast = head; ListNode* lnode = head; ListNode* node = nullptr; //完成上述操作为状态1 //找到中间节点的同时翻转前半部分 while (fast != nullptr && fast->next != nullptr) { lnode = slow; slow = slow->next; fast = fast->next->next; lnode->next = node; node = lnode; } //完成上述操作为状态2 if (fast != nullptr)//奇数个节点的情况 slow = slow->next; //完成上述操作为状态3 while (lnode != nullptr && slow != nullptr) { if (lnode->val != slow->val) return false; lnode = lnode->next; slow = slow->next; } return true;}

# Leetcode226：翻转二叉树

## 代码实现

void invertTree(TreeNode* root) { if (!root) return ; TreeNode* temp = root->left; root->left = root->right; root->right = temp; invertTree(root->left); invertTree(root->right);}
TreeNode* invertTree(TreeNode* root) { if (!root) return nullptr; TreeNode *right = invertTree(root->right); TreeNode *left = invertTree(root->left); root->left = right; root->right = left; return root;}