自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一道新的面試題回文鏈表你會(huì)么?

開發(fā) 前端
在我們的生活中經(jīng)常會(huì)碰到這種回文的結(jié)構(gòu),回文就是反轉(zhuǎn)以后和以前一樣的就是回文結(jié)構(gòu),例如 1->2->3->2->1,我們將它反轉(zhuǎn)之后還是與原鏈表一樣,我們就稱這種鏈表結(jié)構(gòu)為回文結(jié)構(gòu)。

[[431751]]

新題來咯,回文鏈表

回文鏈表

力扣題目鏈接:https://leetcode-cn.com/problems/palindrome-linked-list/

請(qǐng)判斷一個(gè)鏈表是否為回文鏈表。

示例 1:

  • 輸入: 1->2
  • 輸出: false

示例 2:

  • 輸入: 1->2->2->1
  • 輸出: true

思路

數(shù)組模擬

最直接的想法,就是把鏈表裝成數(shù)組,然后再判斷是否回文。

代碼也比較簡(jiǎn)單。如下:

  1. class Solution { 
  2. public
  3.     bool isPalindrome(ListNode* head) { 
  4.         vector<int> vec; 
  5.         ListNode* cur  = head; 
  6.         while (cur) { 
  7.             vec.push_back(cur->val); 
  8.             cur = cur->next
  9.         } 
  10.         // 比較數(shù)組回文 
  11.         for (int i = 0, j = vec.size() - 1; i < j; i++, j--) { 
  12.             if (vec[i] != vec[j]) return false
  13.         } 
  14.         return true
  15.     } 
  16. }; 

上面代碼可以在優(yōu)化,就是先求出鏈表長度,然后給定vector的初始長度,這樣避免vector每次添加節(jié)點(diǎn)重新開辟空間

  1. class Solution { 
  2. public
  3.     bool isPalindrome(ListNode* head) { 
  4.  
  5.         ListNode* cur  = head; 
  6.         int length = 0; 
  7.         while (cur) { 
  8.             length++; 
  9.             cur = cur->next
  10.         } 
  11.         vector<int> vec(length, 0); // 給定vector的初始長度,這樣避免vector每次添加節(jié)點(diǎn)重新開辟空間 
  12.         cur = head; 
  13.         int index = 0; 
  14.         while (cur) { 
  15.             vec[index++] = cur->val; 
  16.             cur = cur->next
  17.         } 
  18.         // 比較數(shù)組回文 
  19.         for (int i = 0, j = vec.size() - 1; i < j; i++, j--) { 
  20.             if (vec[i] != vec[j]) return false
  21.         } 
  22.         return true
  23.     } 
  24. }; 

反轉(zhuǎn)后半部分鏈表

分為如下幾步:

  • 用快慢指針,快指針有兩步,慢指針走一步,快指針遇到終止位置時(shí),慢指針就在鏈表中間位置
  • 同時(shí)用pre記錄慢指針指向節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn),用來分割鏈表
  • 將鏈表分為前后均等兩部分,如果鏈表長度是奇數(shù),那么后半部分多一個(gè)節(jié)點(diǎn)
  • 將后半部分反轉(zhuǎn) ,得cur2,前半部分為cur1
  • 按照cur1的長度,一次比較cur1和cur2的節(jié)點(diǎn)數(shù)值

如圖所示:

代碼如下:

  1. class Solution { 
  2. public
  3.     bool isPalindrome(ListNode* head) { 
  4.         if (head == nullptr || head->next == nullptr) return true
  5.         ListNode* slow = head; // 慢指針,找到鏈表中間分位置,作為分割 
  6.         ListNode* fast = head; 
  7.         ListNode* pre = head; // 記錄慢指針的前一個(gè)節(jié)點(diǎn),用來分割鏈表 
  8.         while (fast && fast->next) { 
  9.             pre = slow; 
  10.             slow = slow->next
  11.             fast = fast->next->next
  12.         } 
  13.         pre->next = nullptr; // 分割鏈表 
  14.  
  15.         ListNode* cur1 = head;  // 前半部分 
  16.         ListNode* cur2 = reverseList(slow); // 反轉(zhuǎn)后半部分,總鏈表長度如果是奇數(shù),cur2比cur1多一個(gè)節(jié)點(diǎn) 
  17.  
  18.         // 開始兩個(gè)鏈表的比較 
  19.         while (cur1) { 
  20.             if (cur1->val != cur2->val) return false
  21.             cur1 = cur1->next
  22.             cur2 = cur2->next
  23.         } 
  24.         return true
  25.     } 
  26.     // 反轉(zhuǎn)鏈表 
  27.     ListNode* reverseList(ListNode* head) { 
  28.         ListNode* temp; // 保存cur的下一個(gè)節(jié)點(diǎn) 
  29.         ListNode* cur = head; 
  30.         ListNode* pre = nullptr; 
  31.         while(cur) { 
  32.             temp = cur->next;  // 保存一下 cur的下一個(gè)節(jié)點(diǎn),因?yàn)榻酉聛硪淖僣ur->next 
  33.             cur->next = pre; // 翻轉(zhuǎn)操作 
  34.             // 更新pre 和 cur指針 
  35.             pre = cur; 
  36.             cur = temp
  37.         } 
  38.         return pre; 
  39.     } 
  40. }; 

其他語言版本

Java

  1. // 方法一,使用數(shù)組 
  2. class Solution { 
  3.     public boolean isPalindrome(ListNode head) { 
  4.         int len = 0; 
  5.         // 統(tǒng)計(jì)鏈表長度 
  6.         ListNode cur = head; 
  7.         while (cur != null) { 
  8.             len++; 
  9.             cur = cur.next
  10.         } 
  11.         cur = head; 
  12.         int[] res = new int[len]; 
  13.         // 將元素加到數(shù)組之中 
  14.         for (int i = 0; i < res.length; i++){ 
  15.             res[i] = cur.val; 
  16.             cur = cur.next
  17.         } 
  18.         // 比較回文 
  19.         for (int i = 0, j = len - 1; i < j; i++, j--){ 
  20.             if (res[i] != res[j]){ 
  21.                 return false
  22.             } 
  23.         } 
  24.         return true
  25.     } 
  26.  
  27. // 方法二,快慢指針 
  28. class Solution { 
  29.     public boolean isPalindrome(ListNode head) { 
  30.         // 如果為空或者僅有一個(gè)節(jié)點(diǎn),返回true 
  31.         if (head == null && head.next == nullreturn true
  32.         ListNode slow = head; 
  33.         ListNode fast = head; 
  34.         ListNode pre = head; 
  35.         while (fast != null && fast.next != null){ 
  36.             pre = slow;  // 記錄slow的前一個(gè)結(jié)點(diǎn) 
  37.             slow = slow.next
  38.             fast = fast.next.next
  39.         } 
  40.         pre.next = null;  // 分割兩個(gè)鏈表 
  41.  
  42.         // 前半部分 
  43.         ListNode cur1 = head; 
  44.         // 后半部分。這里使用了反轉(zhuǎn)鏈表 
  45.         ListNode cur2 = reverseList(slow); 
  46.  
  47.         while (cur1 != null){ 
  48.             if (cur1.val != cur2.val) return false
  49.  
  50.             // 注意要移動(dòng)兩個(gè)結(jié)點(diǎn) 
  51.             cur1 = cur1.next
  52.             cur2 = cur2.next
  53.         } 
  54.         return true
  55.     } 
  56.     ListNode reverseList(ListNode head){ 
  57.         // 反轉(zhuǎn)鏈表 
  58.         ListNode tmp = null
  59.         ListNode pre = null
  60.         while (head != null){ 
  61.             tmp = head.next
  62.             head.next = pre; 
  63.             pre = head; 
  64.             head = tmp; 
  65.         } 
  66.         return pre; 
  67.     } 

 Python

  1. #數(shù)組模擬 
  2. class Solution: 
  3.     def isPalindrome(self, head: ListNode) -> bool: 
  4.         length = 0 
  5.         tmp = head 
  6.         while tmp: #求鏈表長度 
  7.             length += 1 
  8.             tmp = tmp.next 
  9.  
  10.         result = [0] * length 
  11.         tmp = head 
  12.         index = 0 
  13.         while tmp: #鏈表元素加入數(shù)組 
  14.             result[index] = tmp.val 
  15.             index += 1 
  16.             tmp = tmp.next 
  17.  
  18.         i, j = 0, length - 1 
  19.         while i < j: # 判斷回文 
  20.             if result[i] != result[j]: 
  21.                 return False 
  22.             i += 1 
  23.             j -= 1 
  24.         return True 
  25.  
  26. #反轉(zhuǎn)后半部分鏈表 
  27. class Solution: 
  28.     def isPalindrome(self, head: ListNode) -> bool: 
  29.         if head == None or head.next == None: 
  30.             return True 
  31.         slow, fast = head, head 
  32.         while fast and fast.next
  33.             pre = slow 
  34.             slow = slow.next 
  35.             fast = fast.next.next 
  36.  
  37.         pre.next = None # 分割鏈表 
  38.         cur1 = head # 前半部分 
  39.         cur2 = self.reverseList(slow) # 反轉(zhuǎn)后半部分,總鏈表長度如果是奇數(shù),cur2比cur1多一個(gè)節(jié)點(diǎn) 
  40.         while cur1: 
  41.             if cur1.val != cur2.val: 
  42.                 return False 
  43.             cur1 = cur1.next 
  44.             cur2 = cur2.next 
  45.         return True 
  46.  
  47.     def reverseList(self, head: ListNode) -> ListNode: 
  48.         cur = head 
  49.         pre = None 
  50.         while(cur!=None): 
  51.             temp = cur.next # 保存一下cur的下一個(gè)節(jié)點(diǎn) 
  52.             cur.next = pre # 反轉(zhuǎn) 
  53.             pre = cur 
  54.             cur = temp 
  55.         return pre 

 

責(zé)任編輯:姜華 來源: 代碼隨想錄
相關(guān)推薦

2024-10-11 17:09:27

2018-03-06 15:30:47

Java面試題

2011-05-23 11:27:32

面試題面試java

2009-08-11 14:59:57

一道面試題C#算法

2023-02-04 18:24:10

SeataJava業(yè)務(wù)

2009-08-11 10:12:07

C#算法

2009-08-11 15:09:44

一道面試題C#算法

2017-11-21 12:15:27

數(shù)據(jù)庫面試題SQL

2022-04-08 07:52:17

CSS面試題HTML

2023-08-01 08:10:46

內(nèi)存緩存

2021-05-31 07:55:44

smartRepeatJavaScript函數(shù)

2019-09-02 15:06:16

面試字節(jié)跳動(dòng)算法

2022-02-08 18:09:20

JS引擎解析器

2021-03-16 05:44:26

JVM面試題運(yùn)行時(shí)數(shù)據(jù)

2011-03-02 10:58:16

SQL server入門面試題

2015-09-02 14:09:19

面試題程序設(shè)計(jì)

2017-03-10 09:33:16

JavaScript類型

2020-11-06 09:05:18

前端web開發(fā)

2017-09-13 07:15:10

Python讀寫文件函數(shù)

2021-03-27 10:59:45

JavaScript開發(fā)代碼
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)