categories: [Blog,Algorithm]
141. 环形链表
public boolean hasCycle(ListNode head) {
ListNode fast=head,slow = head;
while(fast!=null && fast.next!=null){
fast = fast.next.next;
slow = slow.next;
if (fast == slow){
return true;
}
}
return false;
}
- 回文链表
/**
* 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) {
Stack<Integer> stk =new Stack();
if (null==head || null==head.next) return true;
ListNode p = head;
while (p != null) {
stk.push(p.val);
p = p.next;
}
while (head != null){
if(head.val == stk.peek()) {
stk.pop();
} else return false;
head = head.next;
}
return true;
}
// 作者:wo-yao-chu-qu-luan-shuo
// 链接:https://leetcode-cn.com/problems/palindrome-linked-list/solution/hui-wen-lian-biao-fu-zhu-zhan-by-wo-yao-ab2uc/
}