当前位置:网站首页>Interview question 02.06 Palindrome linked list
Interview question 02.06 Palindrome linked list
2022-06-11 07:07:00 【Not coriander】
Write a function , Check whether the input list is palindromic .
Example 1:
Input : 1->2
Output : false
Example 2:
Input : 1->2->2->1
Output : true
Advanced :
Can you use O(n) Time complexity and O(1) Space complexity to solve this problem ?
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode *head2;
bool f(ListNode*p){
if(p==NULL){
return true;
}
if(f(p->next)==false){
return false;
}
if(p->val!=head2->val)return false;
head2=head2->next;
return true;
}
bool isPalindrome(ListNode* head) {
head2=head;
return f(head);
}
};
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
// Reverse a linked list
ListNode* reverseList(ListNode* head) {
if(head==NULL||head->next==NULL){
return head;
}
ListNode* p=reverseList(head->next);
head->next->next=head;
head->next=NULL;
return p;
}
bool isPalindrome(ListNode* head) {
ListNode *node1=new ListNode(0,head);
ListNode *node2=new ListNode(0,head);
while(node2->next){
node1=node1->next;
// cout<<node1->val<<" ";
node2=node2->next;
if(node2->next){
node2=node2->next;
}
else{
break;
}
}
node1=reverseList(node1->next);
node2=head;
// cout<<endl<<node1->val<<" "<<node2->val<<endl;
while(node1){
if(node1->val!=node2->val){
return false;
}
node1=node1->next;
node2=node2->next;
}
return true;
}
};
边栏推荐
- Shuttle inside and outside margins
- The difference between TCP and UDP
- 1734. arrangement after decoding XOR
- . Net C Foundation (6): namespace - scope with name
- Janus feature草稿
- saltstack的常用模块
- June training (day 11) - matrix
- 顶流编辑器 Atom,将于 12 月 15 日退出历史舞台
- Matplotlib,设置坐标刻度大小,字体/设置图例大小及字体/设置纵横坐标名称及字体及大小
- [matlab WSN communication] a_ Star improved leach multi hop transmission protocol [including source code phase 487]
猜你喜欢

生物序列智能分析平台blog(1)

教育专家王中泽老师:家庭教育重在自己成长

資深OpenStacker - 彭博、Vexxhost昇級為OpenInfra基金會黃金成員

【Matlab WSN通信】A_Star改进LEACH多跳传输协议【含源码 487期】

Biweekly investment and financial report: capital rush yuan universe game

Deep Attentive Tracking via Reciprocative Learning

WPF 数据绑定(四)

Stack -- one of two common linear structures of linear structure

Nodejs database (Part 2)

【LeetCode】-- 17.电话号码的字母组合
随机推荐
Senior openstacker - Bloomberg, vexxhost upgraded to the Gold member of openinfra Foundation
Common modules of saltstack
12. integer to Roman numeral
Comparison of DOM tags of wechat applet development (native and uniapp)
Henan college entrance examination vs Tianjin college entrance examination (2008-2021)
資深OpenStacker - 彭博、Vexxhost昇級為OpenInfra基金會黃金成員
Quality-aware Feature Aggregation Networkfor Robust RGBT Tracking
AtomicInteger原子操作类
Shell脚本之启动Nacos服务端
Practice: how to reasonably design functions to solve practical problems in software development (I)
Flutter Container组件
Starting from scratch (IV) enemy aircraft flying out of the border disappear automatically
Esp32 learning notes (49) - esp-wifi-mesh interface use
Shangtang technology has actively resumed work and will vigorously invest in the capacity and deployment of the digital sentry
The difference between TCP and UDP
Shutter restraint container assembly
Nodejs database (Part 2)
迅为干货 |瑞芯微RK3568开发板TFTP&NFS烧写(上)
农房一体脚本的心得记录
Leetcode hot topic 100 topic 21-25 solution