当前位置:网站首页>12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
2022-04-23 03:13:00 【Big data development of vegetables】
lt.234- Palindrome list
[ Case needs ]

[ Train of thought analysis 1 , Simple breakthrough ]
- After nearly 40 linked list questions , The writer deeply realized , Linked list title and set , Pointers are really too relevant !!!
- For the title of linked list class , If you're trying to traverse a collection ( Traversal is normal , Still use the form of fast and slow needles ) If there is some difficulty , Then use a set or array to store the values of linked list nodes or nodes , To solve a given problem , It's a very simple and direct method ;
- Like this one , Identify whether a linked list is a palindrome linked list , If we directly traverse to compare , It must be a little hard to think of , Then you can remove the nodes of the linked list , The order of the questions ( Because we have to judge whether it conforms to palindromes ), We'll just let him order , Then compare it in the form of colliding two pointers .
- If you select an array , feasible , But you have to traverse the linked list to know the number of nodes , Then we can initialize the array , Then traverse the linked list storage node , Too complicated !
- Let's choose the set , Use a collection that can be accessed randomly , Then it must be list 了
- Topic ideas :
-
- Traversing the linked list , Store the linked list values into the set ( It is also feasible to store nodes directly )
-
- Then hit the double pointer , Opposite traversal list, Comparison is equal , It's not equal , Go straight back to false that will do ;
[ Code implementation one , ]
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public boolean isPalindrome(ListNode head) {
// For this kind of traversal difficult problem , We can give it to the collection , Array
// Put all into the array ( Array doesn't know size , dry ), Then I double pointer collision traversal
// Then I'll put it in list, Then collide
ListNode temp = head;
List<Integer> list = new ArrayList<>();
while(temp != null){
list.add(temp.val);
temp = temp.next;
}
//
int size = list.size();
for(int i = 0; i < size; i++){
int left = i;
int right = size - i - 1;
if(list.get(left) != list.get(right)){
return false;
}
}
return true;
}
}


[ Train of thought analysis II , Speed pointer ]
- Find the tail node of the first half of the linked list , That is, the previous node of the middle node of the whole linked list , First master the writing method of finding the middle node of the linked list : lt. 876. The middle node of a list
- Reverse the second half of linked list , lt. 206 Reverse a linked list
- Judge whether palindrome or not
- Restore the linked list
- Return results
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public boolean isPalindrome(ListNode head) {
//1. Get the intermediate node
//2. Reverse the linked list behind the intermediate node
//3. Traverse from the beginning and back from the intermediate node and compare
//4. Restore the inverted linked list ( Turn it over again )
//1. Get the intermediate node
ListNode middleNode = getMiddleNode(head);
//2. Reverse the linked list behind the node in
ListNode behindMid = reverse(middleNode.next);
//3. Connect
//middleNode.next = behindMid;
//4. Compare nodes
ListNode fromHead = head;
ListNode fromMid = behindMid;
boolean result = true;
while(result && fromMid!= null){
if(fromHead.val != fromMid.val) result = false;
fromHead = fromHead.next;
fromMid = fromMid.next;
}
return result;
}
public ListNode getMiddleNode(ListNode head){
ListNode fast = head;
ListNode slow = head;
while(fast.next != null && fast.next.next != null){
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
// Reverse a linked list
public ListNode reverse(ListNode head){
// Recursive export
if(head == null || head.next == null)return head;
// Recursive entry
ListNode newHead = reverse(head.next);
// Return
head.next.next = head;
head.next = null;
return newHead;
}
}

[ Train of thought Analysis III , Ingenious solution , ]
- See Synonyms at explanation : Am I
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null) {
return true;
}
ListNode slow = head, fast = head;
ListNode pre = head, prepre = null;
while(fast != null && fast.next != null) {
pre = slow;
slow = slow.next;
fast = fast.next.next;
pre.next = prepre;
prepre = pre;
}
if(fast != null) {
slow = slow.next;
}
while(pre != null && slow != null) {
if(pre.val != slow.val) {
return false;
}
pre = pre.next;
slow = slow.next;
}
return true;
}
}
[ Train of thought Analysis III , Recursive method ]
// To be added
版权声明
本文为[Big data development of vegetables]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230310455375.html
边栏推荐
- Mysql database
- 中后二叉建树
- 2022 P cylinder filling training test questions and simulation test
- 全网讲的最细,软件测试度量,怎样优化软件测试成本提高效率---火爆
- Tencent video price rise: earn more than 7.4 billion a year! Pay attention to me to receive Tencent VIP members, and the weekly card is as low as 7 yuan
- js递归树结构计算每个节点的叶子节点的数量并且输出
- Course design of Database Principle -- material distribution management system
- JSON related
- Using positive and negative traversal to solve the problem of "the shortest distance of characters"
- What kind of experience is it to prepare for a month to participate in ACM?
猜你喜欢
![[Mysql] LEFT函數 | RIGHT函數](/img/26/82e0f2280de011636c26931a74e749.png)
[Mysql] LEFT函數 | RIGHT函數

Top ten project management software similar to JIRA
![[new version release] componentone added Net 6 and blazor platform control support](/img/08/71e7328f685a5cdd584f1bfdce5f2a.png)
[new version release] componentone added Net 6 and blazor platform control support

2022A特种设备相关管理(电梯)上岗证题库及模拟考试

研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台

Drawing polygons with < polygon / > circular array in SVG tag
![Eight elder brothers chronicle [4]](/img/87/f695d0275f8a66b9def48a75668d15.png)
Eight elder brothers chronicle [4]

A set of combination boxing to create an idea eye protection scheme

在.NE6 WebApi中使用分布式缓存Redis

編碼電機PID調試(速度環|比特置環|跟隨)
随机推荐
Xamarin effect Chapter 21 expandable floating operation button in GIS
C syntax pattern matching [switch expression]
2022t elevator repair test simulation 100 questions and online simulation test
全网最全,接口自动化测试怎么做的?精通接口自动化测试详解
Huawei mobile ADB devices connection device is empty
[Mysql] LEFT函數 | RIGHT函數
Xutils3 corrected a bug I reported. Happy
C language to achieve address book - (static version)
再战leetcode (290.单词规律)
Mysql database
2022年P气瓶充装培训试题及模拟考试
类似Jira的十大项目管理软件
ASP. Net 6 middleware series - execution sequence
Source code interpretation of Flink index parameters (read quantity, sent quantity, sent bytes, received bytes, etc.)
Fight leetcode again (290. Word law)
Using positive and negative traversal to solve the problem of "the shortest distance of characters"
yes. Net future
js递归树结构计算每个节点的叶子节点的数量并且输出
Top 9 task management system in 2022
Xamarin effect Chapter 22 recording effect