首页 > 学院 > 开发设计 > 正文

234. Palindrome Linked List

2019-11-14 09:18:08
字体:
来源:转载
供稿:网友

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?

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool isPalindrome(ListNode* head) { string s; while(head != NULL){ s.push_back(head->val + '0'); head = head->next; } string t(s); reverse(t.begin(), t.end()); return s == t ? true : false; }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表