基础数据结构—你需要重排链表

开发 前端
链表是一种常见的基础数据结构,结构体指针在这里得到了充分的利用。链表可以动态的进行存储分配,也就是说,链表是一个功能极为强大的数组,他可以在节点中定义多种数据类型,还可以根据需要随意增添.

[[432017]]

重排链表

思路

本篇将给出三种C++实现的方法

  • 数组模拟
  • 双向队列模拟
  • 直接分割链表

方法一

把链表放进数组中,然后通过双指针法,一前一后,来遍历数组,构造链表。

代码如下:

  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; // 计数,偶数去后面,奇数取前面 
  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; // 注意结尾 
  27.     } 
  28. }; 

方法二

把链表放进双向队列,然后通过双向队列一前一后弹出数据,来构造新的链表。这种方法比操作数组容易一些,不用双指针模拟一前一后了

  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; // 计数,偶数去后面,奇数取前面 
  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; // 注意结尾 
  29.     } 
  30. }; 

方法三

将链表分割成两个链表,然后把第二个链表反转,之后在通过两个链表拼接成新的链表。

如图:

 

这种方法,比较难,平均切割链表,看上去很简单,真正代码写的时候有很多细节,同时两个链表最后拼装整一个新的链表也有一些细节需要注意!

代码如下:

  1. class Solution { 
  2. private: 
  3.     // 反转链表 
  4.     ListNode* reverseList(ListNode* head) { 
  5.         ListNode* temp; // 保存cur的下一个节点 
  6.         ListNode* cur = head; 
  7.         ListNode* pre = NULL
  8.         while(cur) { 
  9.             temp = cur->next;  // 保存一下 cur的下一个节点,因为接下来要改变cur->next 
  10.             cur->next = pre; // 翻转操作 
  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.         // 使用快慢指针法,将链表分成长度均等的两个链表head1和head2 
  22.         // 如果总链表长度为奇数,则head1相对head2多一个节点 
  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.         // 对head2进行翻转 
  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; // 偶数取head2的元素,奇数取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) { // 处理结尾 
  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.         //求出中点 
  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.         //反转右部分 right就是反转后右部分的起点 
  15.         right = reverseList(right); 
  16.         //左部分的起点 
  17.         ListNode left = head; 
  18.         //进行左右部分来回连接 
  19.         //这里左部分的节点个数一定大于等于右部分的节点个数 因此只判断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实现,使用数组存储节点 
  45.  class Solution { 
  46.     public void reorderList(ListNode head) { 
  47.         // 双指针的做法 
  48.         ListNode cur = head; 
  49.         // ArrayList底层是数组,可以使用下标随机访问 
  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.                 // 偶数 
  61.                 cur.next = list.get(r); 
  62.                 r--; 
  63.             }else { 
  64.                 // 奇数 
  65.                 cur.next = list.get(l); 
  66.                 l++; 
  67.             } 
  68.             // 每一次指针都需要移动 
  69.             cur = cur.next
  70.             count++; 
  71.         } 
  72.         // 注意结尾要结束一波 
  73.         cur.next = null
  74.     } 
  75. // 方法二:使用双端队列,简化了数组的操作,代码相对于前者更简洁(避免一些边界条件) 
  76. class Solution { 
  77.     public void reorderList(ListNode head) { 
  78.         // 使用双端队列的方法来解决 
  79.         Deque<ListNode> de = new LinkedList<>(); 
  80.         // 这里是取head的下一个节点,head不需要再入队了,避免造成重复 
  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.                 // 偶数,取出队列右边尾部的值 
  92.                 cur.next = de.pollLast(); 
  93.             }else { 
  94.                 // 奇数,取出队列左边头部的值 
  95.                 cur.next = de.poll(); 
  96.             } 
  97.             cur  = cur.next
  98.             count++; 
  99.         } 
  100.         cur.next = null
  101.     } 

 Python

  1. # 方法二 双向队列 
  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: # 链表除了首元素全部加入双向队列 
  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. # 方法三 反转链表 
  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) #反转右半边 
  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的下一个节点 
  50.             cur.next = pre # 反转 
  51.             pre = cur 
  52.             cur = temp 
  53.         return pre 

 

责任编辑:姜华 来源: 代码随想录
相关推荐

2021-08-03 10:24:59

数据跳跃链表结构

2021-05-12 14:09:35

链表数据结构线性结构

2021-07-15 06:43:12

Python数据结构

2021-07-13 07:52:03

Python数据结构

2017-03-01 13:58:46

Python数据结构链表

2021-01-06 08:03:00

JavaScript数据结构

2012-02-02 10:21:05

单链表nexthead

2021-01-28 07:33:34

JavaScript链表数据

2021-04-12 15:47:00

数据结构算法链表

2020-10-21 12:45:12

Redis数据结构

2010-07-13 13:27:13

Perl复杂数据结构

2021-12-21 08:19:29

数据结构算法链表相交

2021-03-10 08:42:19

Java数据结构算法

2023-10-30 08:31:42

数据结构算法

2017-11-14 13:48:26

数据结构学习

2012-10-10 10:30:18

数据结构

2012-10-08 14:52:56

数据结构

2012-10-09 10:09:19

数据结构

2012-10-18 10:40:46

数据结构

2012-10-08 15:59:38

数据结构
点赞
收藏

51CTO技术栈公众号