[LeetCode]234. Palindrome Linked List
来源:互联网 发布:电视怎么弄成网络电视 编辑:程序博客网 时间:2024/06/06 05:46
https://leetcode.com/problems/palindrome-linked-list/
对input进行修改,后半段反转。但是反转链表还是不熟啊,这怎么行!!!反转之后的尾节点的next为null。剩下看注释
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public boolean isPalindrome(ListNode head) { ListNode fast = head; ListNode slow = head; while (fast != null && fast.next != null) { fast = fast.next.next; slow = slow.next; } // 总结点数为奇数,为满足下方while判断,slow要后移一位 if (fast != null) { slow = slow.next; } ListNode tail = reverse(slow); // 不能是head != null,因为前半段的尾节点未置为空 while (tail != null) { if (tail.val != head.val) { return false; } tail = tail.next; head = head.next; } return true; } private ListNode reverse(ListNode head) { ListNode pre = null; while (head != null) { ListNode next = head.next; head.next = pre; pre = head; head = next; } return pre; }}
0 0
- [leetcode] 234.Palindrome Linked List
- [LeetCode]234.Palindrome Linked List
- [leetcode] 234.Palindrome Linked List
- 【leetcode】234. Palindrome Linked List
- 【LeetCode】234. Palindrome Linked List
- [leetcode] 234. Palindrome Linked List
- 234. Palindrome Linked List LeetCode
- LeetCode--234. Palindrome Linked List
- LeetCode 234. Palindrome Linked List
- 【LeetCode】234. Palindrome Linked List
- leetcode 234. Palindrome Linked List
- LeetCode *** 234. Palindrome Linked List
- LeetCode 234. Palindrome Linked List
- leetcode-234. Palindrome Linked List
- LeetCode-234.Palindrome Linked List
- 【LeetCode】234. Palindrome Linked List
- [Leetcode]234. Palindrome Linked List
- 【leetcode】234. Palindrome Linked List
- 数据结构与算法(C语言版)__顺序查找
- spring源码深度解析(笔记三)--自定义标签的解析
- windows下curl的使用
- datatables插件导出excel【指定excel单元格格式,禁止用科学计数法显示】
- git 设置密码
- [LeetCode]234. Palindrome Linked List
- [Math] 矩阵求导规则
- clipboard 实现复制
- 调用记事本使用快捷键
- Java web项目从tomcat转为weblogic环境所遇到的问题
- EditText.setText()无效
- Android 房贷计算
- 【SpringMVC】SpringMVC基础-SpringMVC项目快速搭建、日志框架为logback
- MATLAB中函数bwmorph的用法