tanchukw

Untitled

Jul 29th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. https://leetcode.com/problems/palindrome-linked-list/
  2.  
  3. class Solution
  4. {
  5. private:
  6. ListNode* get_node(ListNode* head, int n)
  7. {
  8. for (int i = 1; i < n; ++i)
  9. {
  10. head = head->next;
  11. }
  12. return head;
  13. }
  14. public:
  15. bool isPalindrome(ListNode* head)
  16. {
  17. int sz = 0, m;
  18. ListNode* t = head;
  19. while (t != NULL)
  20. {
  21. sz++;
  22. t = t->next;
  23. }
  24. m = sz / 2;
  25. t = get_node(head, sz - m + 1);
  26. for (int i = 0; i < m; ++i)
  27. {
  28. if (t->val != get_node(head, m - i)->val)
  29. return 0;
  30. else
  31. t = t->next;
  32. }
  33. return 1;
  34.  
  35. }
  36. };
Advertisement
Add Comment
Please, Sign In to add comment