跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 力扣 回文链表,我写的递归怎么是错的
未分類
26 10 月 2020

力扣 回文链表,我写的递归怎么是错的

力扣 回文链表,我写的递归怎么是错的

資深大佬 : amiwrong123 9

https://leetcode-cn.com/problems/palindrome-linked-list/solution/hui-wen-lian-biao-by-leetcode/

官方答案递归:

class Solution {      private ListNode frontPointer;      private boolean recursivelyCheck(ListNode currentNode) {         if (currentNode != null) {             if (!recursivelyCheck(currentNode.next)) return false;             if (currentNode.val != frontPointer.val) return false;             frontPointer = frontPointer.next;         }         return true;     }      public boolean isPalindrome(ListNode head) {         frontPointer = head;         return recursivelyCheck(head);     } } 

我写的感觉执行过程是一样的啊,看懵了,大佬们帮忙看看。

class Solution {     private ListNode frontPointer;      private boolean recursion(ListNode currentNode){         if (currentNode.next == null){             return true;         }         if (!recursion(currentNode.next))              return false;         boolean result = frontPointer.val == currentNode.val;         frontPointer = frontPointer.next;         return result;     }      public boolean isPalindrome(ListNode head) {         frontPointer = head;         return recursion(head);     } } 

输入: [1,2] 输出: true 预期结果: false

大佬有話說 (1)

  • 主 資深大佬 : amiwrong123

    刚发完贴就发现,自己哪里错了。。。if (currentNode.next == null)应该是 if (currentNode== null)。

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具