当前位置:网站首页>Leetcode234 question - simple method to judge palindrome linked list
Leetcode234 question - simple method to judge palindrome linked list
2022-07-27 16:19:00 【Skinny monkey 117】
Catalog
subject
Give you the head node of a single linked list
head, Please judge whether the linked list is a palindrome linked list . If it is , returntrue; otherwise , returnfalse.
Tips :
The number of nodes in the linked list is in the range [1,105] Inside ,0<= Node.val <= 9
requirement :Time complexity :O(n)
Spatial complexity :O(1)
analysis
To meet the requirements of complexity , We can reverse the sub linked list starting from the intermediate node , Compare the sub linked list with the original linked list , Judge whether it is palindrome list .( At this time, there is no new linked list node , They are all operated on the basis of the original linked list )
First step : Find the middle node of the linked list
// Find the middle point public ListNode middleNode(ListNode head) { ListNode f = head; ListNode l = head; while (f != null && f.next != null) { f = f.next.next; l = l.next; } return l; }
The second step : Reverse the sub linked list starting from the intermediate node
// reverse public ListNode reverList(ListNode head) { if(head ==null||head.next ==null){ return head; } ListNode prev = null; ListNode cur = head; while(cur !=null){ ListNode next = cur.next; cur.next = prev;// Broken wire prev = cur; cur = next; } return prev; }The third step : Compare the values of the original linked list and the sub linked list
Traverse and compare the original linked list and sub linked list
// Ergodic comparison public boolean isPalindrome(ListNode head) { ListNode midHead = middleNode(head); ListNode newHead = reverList(midHead); while (newHead != null) { if (head.val != newHead.val) { return false; } newHead = newHead.next; head = head.next; } return true; }
Code implementation
The three-step method is combined to solve the problem
class Solution { // Ergodic comparison public boolean isPalindrome(ListNode head) { ListNode midHead = middleNode(head); ListNode newHead = reverList(midHead); while (newHead != null) { if (head.val != newHead.val) { return false; } newHead = newHead.next; head = head.next; } return true; } // reverse public ListNode reverList(ListNode head) { if(head ==null||head.next ==null){ return head; } ListNode prev = null; ListNode cur = head; while(cur !=null){ ListNode next = cur.next; cur.next = prev;// Broken wire prev = cur; cur = next; } return prev; } // In the middle public ListNode middleNode(ListNode head) { ListNode f = head; ListNode l = head; while (f != null && f.next != null) { f = f.next.next; l = l.next; } return l; } }
Time complexity :O(n).
Spatial complexity :O(1).
边栏推荐
- Single machine high concurrency model design
- Common problems of mobile terminal H5
- flink打包程序提交任务示例
- 三星关闭在中国最后一家手机工厂
- 解决flink启动后无法正常关闭
- Multiline text overflow dotting
- It can carry 100 people! Musk releases the strongest "starship" in history! Go to Mars as early as next year!
- Example of the task submitted by the Flink packer
- 台积电的反击:指控格芯侵犯25项专利,并要求禁售!
- Servlet basic knowledge points
猜你喜欢

Keil implements compilation with makefile

解决flink启动后无法正常关闭

Servlet basic knowledge points

Understand │ what is cross domain? How to solve cross domain problems?

时间序列——使用tsfresh进行分类任务

Excel extract duplicates

解决MT7620不断循环uboot(LZMA ERROR 1 - must RESET board to recover)

DRF学习笔记(一):数据序列化

Web test learning notes 01

scrapy爬虫框架
随机推荐
Have you ever used the comma operator?
Multiline text overflow dotting
For enterprise operation and maintenance security, use the cloud housekeeper fortress machine!
Penetration test - dry goods | 80 + network security interview experience post (interview)
Determine the exact type of data
时间序列-ARIMA模型
Security software related to wireless network analysis (airtrack ng)
Live broadcast software development, customized pop-up effect of tools
Chuanyin holdings disclosed that it was prosecuted by Huawei: a case has been filed, involving an amount of 20million yuan
逗号操作符你有用过吗?
2.2 basic elements of JMeter
4-digit random data
DeFi安全之DEX与AMMs
解决flink启动后无法正常关闭
台积电的反击:指控格芯侵犯25项专利,并要求禁售!
Time series ARIMA model
Mysql5.7 master-slave hot standby settings on CentOS
渗透测试-干货 | 80篇+网络安全面试经验帖(面试篇)
Enable shallow and deep copies+
C语言程序设计(第三版)


