Leetcode47: Palindrome Linked List
List Linked Palindrome
2023-09-14 09:08:59 时间
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; } };
相关文章
- layer-list实现只有左、右和下边框的圆角矩形
- Singly linked list algorithm implemented by Java
- [TS] Implement a doubly linked list in TypeScript
- Python 入门(四)List和Tuple类型
- LeetCode:114_Flatten Binary Tree to Linked List | 将一棵二叉树变成链表的形式 | Medium
- python的dict,set,list,tuple应用详解
- Python中内置数据类型list,tuple,dict,set的区别和用法
- (LeetCode 203)Remove Linked List Elements
- [TS] Implement a doubly linked list in TypeScript
- [TS] Implement a singly linked list in TypeScript
- [LeetCode] Odd Even Linked List
- Atitit cnchar simp best list 汉字简化方案 最简化汉字256个
- 成功解决AttributeError: 'list' object has no attribute 'ndim'
- List的contains方法使用
- 来说一个技术点,List作为参数数据丢失问题
- AutoCompleteTextView 简单用法 实现自定义list adapter
- leetcode - Reverse Linked List II
- leetcode 19 -- Remove Nth Node From End of List