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

基礎(chǔ)數(shù)據(jù)結(jié)構(gòu)—你需要重排鏈表

開發(fā) 前端
鏈表是一種常見的基礎(chǔ)數(shù)據(jù)結(jié)構(gòu),結(jié)構(gòu)體指針在這里得到了充分的利用。鏈表可以動(dòng)態(tài)的進(jìn)行存儲(chǔ)分配,也就是說,鏈表是一個(gè)功能極為強(qiáng)大的數(shù)組,他可以在節(jié)點(diǎn)中定義多種數(shù)據(jù)類型,還可以根據(jù)需要隨意增添.

[[432017]]

重排鏈表

思路

本篇將給出三種C++實(shí)現(xiàn)的方法

  • 數(shù)組模擬
  • 雙向隊(duì)列模擬
  • 直接分割鏈表

方法一

把鏈表放進(jìn)數(shù)組中,然后通過雙指針法,一前一后,來遍歷數(shù)組,構(gòu)造鏈表。

代碼如下:

  1. class Solution { 
  2. public
  3.     void reorderList(ListNode* head) { 
  4.         vector<ListNode*> vec; 
  5.         ListNode* cur = head; 
  6.         if (cur == nullptr) return
  7.         while(cur != nullptr) { 
  8.             vec.push_back(cur); 
  9.             cur = cur->next
  10.         } 
  11.         cur = head; 
  12.         int i = 1; 
  13.         int j = vec.size() - 1;  // i j為之前前后的雙指針 
  14.         int count = 0; // 計(jì)數(shù),偶數(shù)去后面,奇數(shù)取前面 
  15.         while (i <= j) { 
  16.             if (count % 2 == 0) { 
  17.                 cur->next = vec[j]; 
  18.                 j--; 
  19.             } else { 
  20.                 cur->next = vec[i]; 
  21.                 i++; 
  22.             } 
  23.             cur = cur->next
  24.             count++; 
  25.         } 
  26.         cur->next = nullptr; // 注意結(jié)尾 
  27.     } 
  28. }; 

方法二

把鏈表放進(jìn)雙向隊(duì)列,然后通過雙向隊(duì)列一前一后彈出數(shù)據(jù),來構(gòu)造新的鏈表。這種方法比操作數(shù)組容易一些,不用雙指針模擬一前一后了

  1. class Solution { 
  2. public
  3.     void reorderList(ListNode* head) { 
  4.         deque<ListNode*> que; 
  5.         ListNode* cur = head; 
  6.         if (cur == nullptr) return
  7.  
  8.         while(cur->next != nullptr) { 
  9.             que.push_back(cur->next); 
  10.             cur = cur->next
  11.         } 
  12.  
  13.         cur = head; 
  14.         int count = 0; // 計(jì)數(shù),偶數(shù)去后面,奇數(shù)取前面 
  15.         ListNode* node; 
  16.         while(que.size()) { 
  17.             if (count % 2 == 0) { 
  18.                 node = que.back(); 
  19.                 que.pop_back(); 
  20.             } else { 
  21.                 node = que.front(); 
  22.                 que.pop_front(); 
  23.             } 
  24.             count++; 
  25.             cur->next = node; 
  26.             cur = cur->next
  27.         } 
  28.         cur->next = nullptr; // 注意結(jié)尾 
  29.     } 
  30. }; 

方法三

將鏈表分割成兩個(gè)鏈表,然后把第二個(gè)鏈表反轉(zhuǎn),之后在通過兩個(gè)鏈表拼接成新的鏈表。

如圖:

 

這種方法,比較難,平均切割鏈表,看上去很簡單,真正代碼寫的時(shí)候有很多細(xì)節(jié),同時(shí)兩個(gè)鏈表最后拼裝整一個(gè)新的鏈表也有一些細(xì)節(jié)需要注意!

代碼如下:

  1. class Solution { 
  2. private: 
  3.     // 反轉(zhuǎn)鏈表 
  4.     ListNode* reverseList(ListNode* head) { 
  5.         ListNode* temp; // 保存cur的下一個(gè)節(jié)點(diǎn) 
  6.         ListNode* cur = head; 
  7.         ListNode* pre = NULL
  8.         while(cur) { 
  9.             temp = cur->next;  // 保存一下 cur的下一個(gè)節(jié)點(diǎn),因?yàn)榻酉聛硪淖僣ur->next 
  10.             cur->next = pre; // 翻轉(zhuǎn)操作 
  11.             // 更新pre 和 cur指針 
  12.             pre = cur; 
  13.             cur = temp
  14.         } 
  15.         return pre; 
  16.     } 
  17.  
  18. public
  19.     void reorderList(ListNode* head) { 
  20.         if (head == nullptr) return
  21.         // 使用快慢指針法,將鏈表分成長度均等的兩個(gè)鏈表head1和head2 
  22.         // 如果總鏈表長度為奇數(shù),則head1相對(duì)head2多一個(gè)節(jié)點(diǎn) 
  23.         ListNode* fast = head; 
  24.         ListNode* slow = head; 
  25.         while (fast && fast->next && fast->next->next) { 
  26.             fast = fast->next->next
  27.             slow = slow->next
  28.         } 
  29.         ListNode* head1 = head; 
  30.         ListNode* head2; 
  31.         head2 = slow->next
  32.         slow->next = nullptr; 
  33.  
  34.         // 對(duì)head2進(jìn)行翻轉(zhuǎn) 
  35.         head2 = reverseList(head2); 
  36.  
  37.         // 將head1和head2交替生成新的鏈表head 
  38.         ListNode* cur1 = head1; 
  39.         ListNode* cur2 = head2; 
  40.         ListNode* cur = head; 
  41.         cur1 = cur1->next
  42.         int count = 0; // 偶數(shù)取head2的元素,奇數(shù)取head1的元素 
  43.         while (cur1 && cur2) { 
  44.             if (count % 2 == 0) { 
  45.                 cur->next = cur2; 
  46.                 cur2 = cur2->next
  47.             } else { 
  48.                 cur->next = cur1; 
  49.                 cur1 = cur1->next
  50.             } 
  51.             count++; 
  52.             cur = cur->next
  53.         } 
  54.         if (cur2 != nullptr) { // 處理結(jié)尾 
  55.             cur->next = cur2; 
  56.         } 
  57.         if (cur1 != nullptr) { 
  58.             cur->next = cur1; 
  59.         } 
  60.     } 
  61. }; 

其他語言版本

Java

  1. // 方法三 
  2. public class ReorderList { 
  3.     public void reorderList(ListNode head) { 
  4.         ListNode fast = head, slow = head; 
  5.         //求出中點(diǎn) 
  6.         while (fast.next != null && fast.next.next != null) { 
  7.             slow = slow.next
  8.             fast = fast.next.next
  9.         } 
  10.         //right就是右半部分 12345 就是45  1234 就是34 
  11.         ListNode right = slow.next
  12.         //斷開左部分和右部分 
  13.         slow.next = null
  14.         //反轉(zhuǎn)右部分 right就是反轉(zhuǎn)后右部分的起點(diǎn) 
  15.         right = reverseList(right); 
  16.         //左部分的起點(diǎn) 
  17.         ListNode left = head; 
  18.         //進(jìn)行左右部分來回連接 
  19.         //這里左部分的節(jié)點(diǎn)個(gè)數(shù)一定大于等于右部分的節(jié)點(diǎn)個(gè)數(shù) 因此只判斷right即可 
  20.         while (right != null) { 
  21.             ListNode curLeft = left.next
  22.             left.next = right
  23.             left = curLeft; 
  24.  
  25.             ListNode curRight = right.next
  26.             right.next = left
  27.             right = curRight; 
  28.         } 
  29.     } 
  30.  
  31.     public ListNode reverseList(ListNode head) { 
  32.         ListNode headNode = new ListNode(0); 
  33.         ListNode cur = head; 
  34.         ListNode next = null
  35.         while (cur != null) { 
  36.             next = cur.next
  37.             cur.next = headNode.next
  38.             headNode.next = cur; 
  39.             cur = next
  40.         } 
  41.         return headNode.next
  42.     } 
  43.  
  44. // 方法一 Java實(shí)現(xiàn),使用數(shù)組存儲(chǔ)節(jié)點(diǎn) 
  45.  class Solution { 
  46.     public void reorderList(ListNode head) { 
  47.         // 雙指針的做法 
  48.         ListNode cur = head; 
  49.         // ArrayList底層是數(shù)組,可以使用下標(biāo)隨機(jī)訪問 
  50.         List<ListNode> list = new ArrayList<>(); 
  51.         while (cur != null){ 
  52.             list.add(cur); 
  53.             cur = cur.next
  54.         } 
  55.         cur = head;  // 重新回到頭部 
  56.         int l = 1, r = list.size() - 1;  // 注意左邊是從1開始 
  57.         int count = 0; 
  58.         while (l <= r){ 
  59.             if (count % 2 == 0){ 
  60.                 // 偶數(shù) 
  61.                 cur.next = list.get(r); 
  62.                 r--; 
  63.             }else { 
  64.                 // 奇數(shù) 
  65.                 cur.next = list.get(l); 
  66.                 l++; 
  67.             } 
  68.             // 每一次指針都需要移動(dòng) 
  69.             cur = cur.next
  70.             count++; 
  71.         } 
  72.         // 注意結(jié)尾要結(jié)束一波 
  73.         cur.next = null
  74.     } 
  75. // 方法二:使用雙端隊(duì)列,簡化了數(shù)組的操作,代碼相對(duì)于前者更簡潔(避免一些邊界條件) 
  76. class Solution { 
  77.     public void reorderList(ListNode head) { 
  78.         // 使用雙端隊(duì)列的方法來解決 
  79.         Deque<ListNode> de = new LinkedList<>(); 
  80.         // 這里是取head的下一個(gè)節(jié)點(diǎn),head不需要再入隊(duì)了,避免造成重復(fù) 
  81.         ListNode cur = head.next
  82.         while (cur != null){ 
  83.             de.offer(cur); 
  84.             cur = cur.next
  85.         } 
  86.         cur = head;  // 回到頭部 
  87.  
  88.         int count = 0; 
  89.         while (!de.isEmpty()){ 
  90.             if (count % 2 == 0){ 
  91.                 // 偶數(shù),取出隊(duì)列右邊尾部的值 
  92.                 cur.next = de.pollLast(); 
  93.             }else { 
  94.                 // 奇數(shù),取出隊(duì)列左邊頭部的值 
  95.                 cur.next = de.poll(); 
  96.             } 
  97.             cur  = cur.next
  98.             count++; 
  99.         } 
  100.         cur.next = null
  101.     } 

 Python

  1. # 方法二 雙向隊(duì)列 
  2. class Solution: 
  3.     def reorderList(self, head: ListNode) -> None: 
  4.         ""
  5.         Do not return anything, modify head in-place instead
  6.         ""
  7.         d = collections.deque() 
  8.         tmp = head 
  9.         while tmp.next: # 鏈表除了首元素全部加入雙向隊(duì)列 
  10.             d.append(tmp.next
  11.             tmp = tmp.next 
  12.         tmp = head 
  13.         while len(d): # 一后一前加入鏈表 
  14.             tmp.next = d.pop() 
  15.             tmp = tmp.next 
  16.             if len(d): 
  17.                 tmp.next = d.popleft() 
  18.                 tmp = tmp.next 
  19.         tmp.next = None # 尾部置空 
  20.  
  21. # 方法三 反轉(zhuǎn)鏈表 
  22. class Solution: 
  23.     def reorderList(self, head: ListNode) -> None: 
  24.         if head == None or head.next == None: 
  25.             return True 
  26.         slow, fast = head, head 
  27.         while fast and fast.next
  28.             slow = slow.next 
  29.             fast = fast.next.next 
  30.         right = slow.next # 分割右半邊 
  31.         slow.next = None # 切斷 
  32.         right = self.reverseList(right) #反轉(zhuǎn)右半邊 
  33.         left = head 
  34.         # 左半邊一定比右半邊長, 因此判斷右半邊即可 
  35.         while right
  36.             curLeft = left.next 
  37.             left.next = right 
  38.             left = curLeft 
  39.  
  40.             curRight = right.next 
  41.             right.next = left 
  42.             right = curRight 
  43.  
  44.  
  45.     def reverseList(self, head: ListNode) -> ListNode: 
  46.         cur = head 
  47.         pre = None 
  48.         while(cur!=None): 
  49.             temp = cur.next # 保存一下cur的下一個(gè)節(jié)點(diǎn) 
  50.             cur.next = pre # 反轉(zhuǎn) 
  51.             pre = cur 
  52.             cur = temp 
  53.         return pre 

 

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

2021-08-03 10:24:59

數(shù)據(jù)跳躍鏈表結(jié)構(gòu)

2021-05-12 14:09:35

鏈表數(shù)據(jù)結(jié)構(gòu)線性結(jié)構(gòu)

2021-07-15 06:43:12

Python數(shù)據(jù)結(jié)構(gòu)

2017-03-01 13:58:46

Python數(shù)據(jù)結(jié)構(gòu)鏈表

2021-07-13 07:52:03

Python數(shù)據(jù)結(jié)構(gòu)

2021-01-06 08:03:00

JavaScript數(shù)據(jù)結(jié)構(gòu)

2012-02-02 10:21:05

單鏈表nexthead

2021-01-28 07:33:34

JavaScript鏈表數(shù)據(jù)

2021-04-12 15:47:00

數(shù)據(jù)結(jié)構(gòu)算法鏈表

2020-10-21 12:45:12

Redis數(shù)據(jù)結(jié)構(gòu)

2021-03-10 08:42:19

Java數(shù)據(jù)結(jié)構(gòu)算法

2023-10-30 08:31:42

數(shù)據(jù)結(jié)構(gòu)算法

2010-07-13 13:27:13

Perl復(fù)雜數(shù)據(jù)結(jié)構(gòu)

2021-12-21 08:19:29

數(shù)據(jù)結(jié)構(gòu)算法鏈表相交

2017-11-14 13:48:26

數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)

2012-10-08 15:59:38

數(shù)據(jù)結(jié)構(gòu)

2012-10-10 10:13:22

數(shù)據(jù)結(jié)構(gòu)

2012-10-18 10:40:46

數(shù)據(jù)結(jié)構(gòu)

2012-10-10 10:30:18

數(shù)據(jù)結(jié)構(gòu)

2012-10-09 10:09:19

數(shù)據(jù)結(jié)構(gòu)
點(diǎn)贊
收藏

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