当前位置:网站首页>Palindrome linked list (copy linked list to array, speed pointer + reverse linked list)
Palindrome linked list (copy linked list to array, speed pointer + reverse linked list)
2022-04-22 09:04:00 【There is no limit to learning】
Topic link :https://leetcode-cn.com/problems/palindrome-linked-list/
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 , return true ; otherwise , return false .
Method 1 : Copy the linked list value to the array , Use double pointer to judge whether palindrome
Their thinking :
The code is as follows :
class Solution {
public boolean isPalindrome(ListNode head) {
List<Integer> list = new ArrayList<Integer>();// Create dynamic array
// Copy the value of the linked list to the array
while(head != null){
list.add(head.val);
head = head.next;
}
// Use double pointer to judge whether palindrome is true
int front = 0;
int back = list.size()-1;
while(front < back){
if (!list.get(front).equals(list.get(back))) return false;// Basic palindrome judgment operation
front++;
back--;
}
return true;
}
}
Method 2 : Speed pointer + Reverse a linked list
Their thinking : Use the fast and slow pointer method to find the middle node of the linked list , Reverse the linked list of the second half of the node . Then traverse , Whether the corresponding values are equal .
public static boolean isPalindrome(ListNode head) {
// The first half is the tail node of the linked list ,half
// And reverse to half.next It is the linked list of the second half of the head node
ListNode half = findHalf(head);
ListNode half_next = reverseList(half.next);
// Next, judge whether to reply
ListNode p = head;
ListNode q = half_next;
boolean huiwen = true;
while (huiwen && q != null){
if (p.val != q.val) huiwen = false;
p = p.next;
q = q.next;
}
// Remember to restore the linked list
half.next = reverseList(half_next);// General interviewers do not want to change the linked list structure , So go back
return huiwen;
}
public static ListNode reverseList(ListNode head){
// Reverse the basic operation of the linked list
// Do not understand the reverse linked list iterative method https://blog.csdn.net/weixin_46184836/article/details/123512839
ListNode pre = null;
ListNode cur = head;
ListNode next;
while(cur != null){
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
public static ListNode findHalf(ListNode head){
// When the fast pointer reaches the end , The slow pointer reaches the intermediate node
ListNode slow = head;
ListNode fast = head;
while(fast.next != null && fast.next.next != null){
slow = slow.next;//slow The pointer goes one step at a time
fast = fast.next.next;//fast The pointer takes two steps at a time
}
return slow;
}
版权声明
本文为[There is no limit to learning]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220816567347.html
边栏推荐
- Solve the problem that the disk has space but cannot create files --- repair the server file system
- SQL窗口函数
- Mycms self media CMS system v3 2.2. Advertisement plug-in optimization
- C语言如何设置随机数
- Eventbridge integrated cloud service practice
- VS编译器注释风格
- RHCSA第一天作业
- 花书《深度学习》与代码实现:01 线性代数:基本概念+代码实现基本运算
- How to download pycharm
- quartz中@Scheduled cron表达式
猜你喜欢
随机推荐
51 单片机学习_3-3 独立按键控制LED二进制显示
How to set random number in C language
QT file reading and writing practical tutorial
getchar函数的返回类型
二分查找【详解】
Meaning of GMT and CST in programming
Leetcode0396. Rotation function (medium, iteration)
51 单片机学习_3-2 独立按键控制LED状态
悬空else问题
Flume 组成,Put 事务,Take 事务
Insert sorting and optimization
pip install shutil 报错
那些年不会做的数学题,用Pyhon只需要1分钟?
宝宝起名神器小程序源码_支持多种流量主模式
合并两个有序链表(迭代)
如何解决项目文档管理中的复杂性?
分布式场景业务操作日志实现(基于redis轻量)
学习RHCSA的第一天
Shrimp noodles: what is zero copy? How to achieve zero copy?
知识点的5W









