Leetcode47: Palindrome Linked List
生活随笔
收集整理的這篇文章主要介紹了
Leetcode47: Palindrome Linked List
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Given a singly linked list, determine if it is a palindrome.
推斷一個鏈表是不是回文的,一個比較簡單的辦法是把鏈表每一個結點的值存在vector里。然后首尾比較。時間復雜度O(n)。空間復雜度O(n)。
/*** 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) {vector<int> temp;ListNode* ptr = head;while(ptr!=NULL){temp.push_back(ptr->val);ptr = ptr->next;}int n = temp.size();for(int i = 0; i < n/2; i++){if(temp[i] != temp[n-1-i])return false;}return true;} };
總結
以上是生活随笔為你收集整理的Leetcode47: Palindrome Linked List的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Leetcode][第1392题][J
- 下一篇: 计算机毕业设计Java在线小说系统(源码