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
|
struct ListNode* (struct ListNode* head){ struct ListNode* pre = NULL; struct ListNode* cur = head; struct ListNode* next = NULL; while (cur != NULL){ next = cur->next; cur->next = pre; pre = cur; cur = next; } return pre; }
bool isPalindrome(struct ListNode* head){ if (head == NULL || head->next == NULL) { return true; } struct ListNode* fast = head, * slow = head; while (fast->next !=NULL && fast->next->next != NULL) { slow = slow->next; fast = fast->next->next; } struct ListNode* p = slow->next; slow->next = NULL; p = reverse(p); while (head != NULL && p!= NULL) { if (head->val != p->val){ return false; } head = head->next; p = p->next; } return true; }
|
近期评论