思路1:遍历一次链表,用一个数组存储链表节点值,然后用双指针法判断数组是否是回文的。需要额外O(n)的空间。 C++ classSolution{public:boolisPalindrome(ListNode* head){if(!head || !head->next)returntrue; vector<int> vec;while(head) { vec.push_back(head->val
这一点太弱了。 booljudge(structListNode *p,structListNode *q) {while( p &&q) {if(p->val != q->val)returnfalse; p= p->next; q= q->next; }returnp==NULL && q==NULL; }boolisPalindrome_using_count(structListNode*head) {inti,n =0;structListNode * p = head, *last = NULL, *...
AI代码解释 classSolution{publicbooleanisPalindrome(ListNode head){ListNode fast=head;ListNode slow=head;if(fast==null||fast.next==null)returntrue;while(fast.next!=null&&fast.next.next!=null){fast=fast.next.next;slow=slow.next;}ListNode newHead=reverseList(slow.next);while(newHead!=null){if(...
Leetcode 234 Palindrome Linked List 复杂度为时间O(n) 和空间(1)解法,1.问题描写叙述给定一个单链表,推断其内容是不是回文类型。比如1–>2–>3–>2–>1。时间和空间复杂都尽量低。2.方法与思路1)比較朴素的算法。因为给定的数据结构是单链表,要訪问链表的尾部元素,
LeetCode: 234. Palindrome Linked List 题目描述 Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false 1. 2. Example 2: Input: 1->2->2->1 Output: true 1. 2. Follow up: Could you do it in O(n) time and ...
LeetCode刷题日记 Day 18 Part 2 - Palindrome Linked List, 视频播放量 48、弹幕量 0、点赞数 0、投硬币枚数 0、收藏人数 0、转发人数 0, 视频作者 blackwoodkane, 作者简介 ,相关视频:LeetCode刷题日记 Day 32 Part 1 - Insert Interval,LeetCode刷题日记 Day 4 Part
234. Palindrome Linked List 开始April的daily leetcode。 直接考虑O(n)时间 O(1)空间的做法,假设允许改动List nodes。 容易想到用双指针去跑,这样可以找到中点。在跑的过程中将前半截reverse一下。然后两个指针分别往两边不断next比较。 可以自己画图观察,例如3个节点的list,第一次p跑到1,q跑到2;第二次q在...
原题链接:https://leetcode.com/problems/palindrome-linked-list/ 我自己的想法是把这个链表复制一份然后倒过来,对比一下两个链表是否一样。想法简单但是操作起来非常复杂···链表的复制和比较都用自定义func来实现的。 顺便总结一下copy.copy和copy.deepcopy的区别 ...
LeetCode - 234 - Palindrome Linked List 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? 判断一个链表是不是回文,我的方法是把前一半链表翻转,然后再同时扫描,判断值是否相等。时间复杂度O(n),至于空间复杂度,我觉得...
LeetCode problem solving notes. Determine whether a linked list is a palindrome linked list. First convert the linked list into an array, and then judge whether the array is a palindrome array.