categories: [Blog,Algorithm]


141. 环形链表

  1. public boolean hasCycle(ListNode head) {
  2. ListNode fast=head,slow = head;
  3. while(fast!=null && fast.next!=null){
  4. fast = fast.next.next;
  5. slow = slow.next;
  6. if (fast == slow){
  7. return true;
  8. }
  9. }
  10. return false;
  11. }
  1. 回文链表 ```java /**

    • 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 stk =new Stack(); if (null==head || null==head.next) return true; ListNode p = head;

      while (p != null) {

      1. stk.push(p.val);
      2. p = p.next;

      } while (head != null){

      1. if(head.val == stk.peek()) {
      2. stk.pop();
      3. } else return false;
      4. 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/

}

```