#Leetcode# 234. Palindrome Linked List
https://leetcode.com/problems/palindrome-linked-list/
Given a singly linked list, determine if it is a palindrome.
Example 1:
Input: 1->2 Output: false
Example 2:
Input: 1->2->2->1 Output: true
代码:
/**
* 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) {
if(!head) return true;
string s = "";
ListNode *cur = head;
while(cur) {
s += (cur -> val) + '0';
cur = cur -> next;
}
string ss = s;
reverse(s.begin(), s.end());
if(ss == s) return true;
return false;
}
};
妈耶 自己写对的第一个链表 也太开心了 8!!!!!!! 今日份的七彩开心

浙公网安备 33010602011771号