当前位置:网站首页>Linked list palindrome judgment
Linked list palindrome judgment
2022-07-27 06:07:00 【The last tripod】
link : Palindrome structure of linked list
Ideas : If it's a competition , Turn the linked list directly into an array to do , Convenient and quick . As an interview question , With the restrictions of the interviewer, it's not enough . Because this is a one-way linked list , There is no way to traverse to the last point and then traverse from the back to the front , So we must traverse to the intermediate node first , Then flip the linked list from the intermediate node to the last node
import java.util.*;
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class PalindromeList {
public boolean chkPalindrome(ListNode head) {
// Find the intermediate node
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
}
// Flip the second half of the linked list
ListNode pre = slow;
slow = slow.next;
pre.next = null;
while(slow != null){
ListNode tmp = slow.next;
slow.next = pre;
pre = slow;
slow = tmp;
}
// Comparison is equal
while(head != null && pre != null){
if(head.val != pre.val)return false;
head = head.next;
pre = pre.next;
}
return true;
}
}边栏推荐
- geonode geoserver win10 安装教程(亲测)
- [MVC Architecture] MVC model
- 面试常问Future、FutureTask和CompletableFuture
- What has been updated in the Chinese version of XMIND mind map 2022 v12.0.3?
- A photo breaks through the face recognition system: you can nod your head and open your mouth, netizens
- [song] rebirth of me in py introductory training (10): numpy
- 2022.6.10 stm32mp157 serial port clock learning
- 【头歌】重生之我在py入门实训中(8): 模块
- 安全帽反光衣检测识别数据集和yolov5模型
- PZK学C语言之数据类型,进制转换,输入输出,运算符,分支语句ifelse
猜你喜欢

C语言扫雷最新 递归展开 超详解(附源码)

Kaggle调用自定义模块方法

能替代ps的修图软件?

QGIS系列(1)-QGIS(server-apache) win10安装

Xmind 思维导图 2022 v12.0.3中文版更新了哪些内容?

Super remote connection management tool: Royal TSX

ps 2022 六月更新,都新增了哪些功能
![[Haowen planting grass] knowledge of root domain name - Ruan Yifeng's Weblog](/img/75/8f41db9f9c077b43751d63b7b5b57e.png)
[Haowen planting grass] knowledge of root domain name - Ruan Yifeng's Weblog

李宏毅 2020 深度学习与人类语言处理 DLHLP-Coreference Resolution-p21

LaTeX中多个公式公用一个序号时
随机推荐
根据SQL必知必会学习SQL(MYSQL)
Unittest套件与运行器
Pix2Pix原理解析
pytorch转onnx相关问题
arcgis for js api(1) 获取featureLayer的所有字段名
数据库索引的一些说明以及使用
Baiwen driver Daquan learning (I) LCD driver
Chrome 如何快速将一组正在浏览的网页(tabs)转移到另一台设备(电脑)上
Weidongshan digital photo frame project learning (II) displaying Chinese characters on LCD
【头歌】重生之机器学习-线性回归
服务器相关的指标解释
编程学习记录——第4课【分支和循环语句】
方差与协方差
Greedy high performance neural network and AI chip application research and training
socket编程二:使用select
力扣题解 动态规划(3)
[song] rebirth of me in py introductory training (12): Matplotlib interface and common graphics
acwing每日一题 正方形数组的数目
使用-Wall清除代码隐患
c语言-线性顺序表