当前位置:网站首页>leetcode234题-简单方法判断回文链表
leetcode234题-简单方法判断回文链表
2022-07-27 14:36:00 【瘦皮猴117】
目录
题目
给你一个单链表的头节点
head,请你判断该链表是否为回文链表。如果是,返回true;否则,返回false。
提示:
链表中节点数目在范围 [1,105] 内,0<= Node.val <= 9
要求:时间复杂度:O(n)
空间复杂度:O(1)
分析
要满足复杂度的要求,我们可以反转从中间节点开始的子链表,将子链表与原链表对比,判断是否为回文链表。(此时并没有开辟新的链表节点,都是在原链表基础上操作的)
第一步:找出链表的中间结点
//找出中间点 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; }
第二步:反转从中间结点开始的子链表
//反转 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;//断线 prev = cur; cur = next; } return prev; }第三步:对比原链表与子链表的值
遍历对比原链表与子链表
//遍历对比 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; }
代码实现
三步方法结合解出题目答案
class Solution { //遍历对比 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; } //反转 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;//断线 prev = cur; cur = next; } return prev; } //中间点 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; } }
时间复杂度:O(n)。
空间复杂度:O(1)。
边栏推荐
- 【云享读书会第13期】音频文件的封装格式和编码格式
- C:浅谈函数
- js操作dom节点
- Jump to the specified position when video continues playing
- C language: string function and memory function
- Spark 任务Task调度异常分析
- Spark 3.0 testing and use
- Push down of spark filter operator on parquet file
- 【剑指offer】面试题41:数据流中的中位数——大、小堆实现
- Summary of network device hard core technology insider router (Part 2)
猜你喜欢

Hyperlink parsing in MD: parsing `this$ Set() `, ` $` should be preceded by a space or escape character`\`

多线程带来的的风险——线程安全

使用Lombok导致打印的tostring中缺少父类的属性

初识结构体

First understanding of structure

C language: function stack frame

C语言:数据的存储

CAS compares the knowledge exchanged, ABA problems, and the process of lock upgrading

Is the array name the address of the first element?

C language: dynamic memory function
随机推荐
$router.back(-1)
[system programming] process, thread problem summary
Huawei's general card identification function enables multiple card bindings with one key
使用双星号代替Math.pow()
[正则表达式] 匹配开头和结尾
Causes and solutions of deadlock in threads
聊聊ThreadLocal
Summer Challenge harmonyos realizes a hand-painted board
Network principle (1) - overview of basic principles
Spark 3.0 测试与使用
初识结构体
/Dev/loop1 takes up 100% of the problem
【云享读书会第13期】音频文件的封装格式和编码格式
[regular expression] match the beginning and end
C:浅谈函数
线程间等待与唤醒机制、单例模式、阻塞队列、定时器
Using Prometheus to monitor spark tasks
C language: factorial recursive implementation of numbers
Set the position of the prompt box to move with the mouse, and solve the problem of incomplete display of the prompt box
Spark 3.0 DPP implementation logic


