zoukankan      html  css  js  c++  java
  • 234. Palindrome Linked List(LeetCode)

    Given a singly linked list, determine if it is a palindrome.

    Follow up:
    Could you do it in O(n) time and O(1) space?

     1 /**
     2  * Definition for singly-linked list.
     3  * struct ListNode {
     4  *     int val;
     5  *     ListNode *next;
     6  *     ListNode(int x) : val(x), next(NULL) {}
     7  * };
     8  */
     9 class Solution {
    10 public:
    11     bool isPalindrome(ListNode* head) {
    12        ListNode *p1 = head;
    13         vector<int> vet;
    14         if (head == NULL)
    15             return true;
    16         else
    17         {
    18             while (p1)
    19             {
    20                 vet.push_back(p1->val);
    21                 p1 = p1->next;
    22             }
    23         }
    24         vector<int> vet1;
    25         for (int i = vet.size()-1; i >= 0; --i)
    26         {
    27             vet1.push_back(vet[i]);
    28         }       
    29         if (vet1 == vet)
    30             return true;
    31         else
    32             return false;
    33     }
    34 };
  • 相关阅读:
    求最低价格
    A*算法入门
    hdu 4715
    手动扩大栈内存,让AC无忧
    hdu 4710
    hdu 1698
    poj3468区间延迟更新模板题
    hdu 1059二进制优化背包问题
    2059龟兔赛跑
    水1276
  • 原文地址:https://www.cnblogs.com/wujufengyun/p/7124963.html
Copyright © 2011-2022 走看看