当前位置:网站首页>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
边栏推荐
猜你喜欢
How does Microsoft solve the problem of multiple PC programs
be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?
Blazor University (11) component - replace attributes of subcomponents
Blazor University (12) - component lifecycle
C语言实现通讯录----(静态版本)
Charles uses three ways to modify requests and responses
2022T电梯修理考试模拟100题及在线模拟考试
Drawing polygons with < polygon / > circular array in SVG tag
2022山东省安全员C证上岗证题库及在线模拟考试
IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
随机推荐
Due to 3 ²+ four ²= five ², Therefore, we call '3,4,5' as the number of Pythagorean shares, and find the array of all Pythagorean shares within n (including n).
ASP. Net 6 middleware series - Custom middleware classes
数据挖掘系列(3)_Excel的数据挖掘插件_估计分析
. net tip: talk about the problem that the scoped service cannot be obtained in the middleware structure
建立与遍历二叉树
“如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
Student achievement management
全网讲的最细,软件测试度量,怎样优化软件测试成本提高效率---火爆
[mock data] fastmock dynamically returns the mock content according to the incoming parameters
7-11 重排链表 (25 分)
Configure automatic implementation of curd projects
一套组合拳,打造一款 IDEA 护眼方案
The whole network is the most complete. How to do interface automation test? Proficient in interface automation test details
[untitled]
【VS Code】解决jupyter文件在vs code中显示异常的问题
手机连接电脑后,QT的QDIR怎么读取手机文件路径
全网最全,接口自动化测试怎么做的?精通接口自动化测试详解
Realize QQ login with PHP
be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?
JSON related