当前位置:网站首页>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
边栏推荐
猜你喜欢

TP5 customization in extend directory succeeded and failed. Return information

Web Course Design - his system

C language to achieve address book - (static version)

. net tip: talk about the problem that the scoped service cannot be obtained in the middleware structure

宁德时代地位不保?

Blazor University (11) component - replace attributes of subcomponents
![[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

Laravel8- use JWT

2022年P气瓶充装培训试题及模拟考试

Top ten project management software similar to JIRA
随机推荐
Swap the left and right of each node in a binary tree
Eight elder brothers chronicle [4]
Laravel new route file
ASP. Net 6 middleware series - execution sequence
荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比
js递归树结构计算每个节点的叶子节点的数量并且输出
[authentication / authorization] customize an authentication handler
2022A特种设备相关管理(电梯)上岗证题库及模拟考试
The backtracking of stack is used to solve the problem of "the longest absolute path of file"
Xamarin effect Chapter 21 expandable floating operation button in GIS
C read / write binary file
Ningde's position in the times is not guaranteed?
The most understandable life cycle of dependency injection
可以接收多种数据类型参数——可变参数
Find the number of leaf nodes of binary tree
Source generator actual combat
宁德时代地位不保?
Yes Redis using distributed cache in NE6 webapi
Blazor University (12) - component lifecycle
2022年度Top9的任务管理系统