LeetCode 143题:Reorder List的分析及两种解题思路

特别说明:参考了很多前辈的文章,整理如下,我只做了重新编码的工作,不能保证代码最优,主要用作交流学习。

题目:

方法1:将链表的每个节点地址保存在指针数组中,利用数组随机访问调整链表。

 1 struct ListNode
 2 {
 3     int val;
 4     ListNode *next;
 5     ListNode(int x) : val(x), next(NULL) {}
 6 };
 7
 8 void reorderList(ListNode *head)
 9 {
10     if (head == nullptr)
11     {
12         return;
13     }
14
15     vector<ListNode *> vecNode;
16     vector<ListNode *>::size_type left, right;
17     ListNode *p = head;
18     while (p)
19     {
20         vecNode.push_back(p);
21         p = p->next;
22     }
23
24     left = 0;
25     right = vecNode.size() - 1;
26     while (left < right)
27     {
28         vecNode[left++]->next = vecNode[right];
29         vecNode[right--]->next = vecNode[left];
30     }
31     vecNode[left]->next = nullptr;
32 }

方法2:利用快慢两个指针将链表一分为二,针对第二个子链表求倒序,最后将两个子链表合并。

 1 class Solution {
 2 public:
 3     ListNode *GetMid(ListNode *head)                    //找到链表中点
 4     {
 5         if (head == nullptr || head->next == nullptr)
 6         {
 7             return head;
 8         }
 9
10         ListNode *slow = head;
11         ListNode *fast = head->next;
12
13         while (fast && fast->next)
14         {
15             slow = slow->next;
16             fast = fast->next->next;
17         }
18         return slow;
19     }
20
21     ListNode *ReverseList(ListNode *head)                //链表逆置
22     {
23         if (head == nullptr || head->next == nullptr)
24         {
25             return head;
26         }
27
28         ListNode *p = head;
29         ListNode *q = head->next;
30         ListNode *tmp = nullptr;
31
32         while (q)
33         {
34             tmp = q->next;
35             q->next = p;
36             p = q;
37             q = tmp;
38         }
39         head->next = nullptr;
40
41         return p;
42     }
43
44     ListNode *UnionList(ListNode *head1, ListNode *head2)        //合并链表
45     {
46         if (head1 == nullptr)
47         {
48             return head2;
49         }
50         if (head2 == nullptr)
51         {
52             return head1;
53         }
54
55         ListNode *p = head1;
56         ListNode *q = head2;
57         ListNode *tmpp = nullptr;
58         ListNode *tmpq = nullptr;
59
60         while (p && q)
61         {
62             tmpp = p->next;
63             tmpq = q->next;
64
65             p->next = q;
66             q->next = tmpp;
67
68             p = tmpp;
69             q = tmpq;
70         }
71
72         return head1;
73     }
74
75     void reorderList(ListNode *head)                            //链表重排
76     {
77         if (head == nullptr || head->next == nullptr)
78         {
79             return;
80         }
81
82         ListNode *head1 = nullptr;
83         ListNode *head2 = nullptr;
84         ListNode *mid = nullptr;
85
86         mid = GetMid(head);
87         head2 = mid->next;
88         mid->next = nullptr;                    //分割成两个链表
89
90         head2 = ReverseList(head2);
91         head1 = head;
92         head = UnionList(head1, head2);
93     }
94
95 };

测试你的代码:

这里我提供一种比较好的方法,就是从文件中读入测试数据(多行),然后再分别对每行数据进行reorderList,这样可以方便同时测试多组数据。参考代码如下:

 1 void CreateList(ListNode **head, ListNode **tail, ListNode *n)                //创建链表
 2 {
 3     if (n != NULL)
 4     {
 5         if (*head == NULL)
 6         {
 7             *head = n;
 8             *tail = n;
 9             (*tail)->next = NULL;
10         }
11         else
12         {
13             (*tail)->next = n;
14             (*tail) = n;
15             (*tail)->next = NULL;
16         }
17     }
18 }
19
20 int main(void)
21 {
22     ifstream in("data.txt");                            //打开data文件
23     string line;
24     int val;
25     ListNode *head = NULL;
26     ListNode *tail = NULL;
27     ListNode *n = NULL;
28
29     while (getline(in, line))                            //每次读取一行数据至line中
30     {
31         istringstream clin(line);
32         while (clin >> val)                                //从每行数据中每次读取一个数据
33         {
34             n = new ListNode(val);
35             CreateList(&head, &tail, n);
36         }
37
38         Solution List;
39         List.reorderList(head);
40
41         while (head != NULL)
42         {
43             cout << head->val << ‘ ‘;
44             head = head->next;
45         }
46         cout << endl;
47
48     }
49
50     return 0;
51 }

效果如下:

说明:通过阅读很多优秀的博客,采用方法2的最多,简单清晰。

时间: 2024-10-22 12:31:23

LeetCode 143题:Reorder List的分析及两种解题思路的相关文章

LeetCode算法题-Move Zeroes(Java实现-三种解法)

这是悦乐书的第201次更新,第211篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第67题(顺位题号是283).给定一个数组nums,写一个函数将所有0移动到它的末尾,同时保持非零元素的相对顺序.例如: 输入:[0,1,0,3,12] 输出:[1,3,12,0,0] 注意: 您必须在不制作数组副本的情况下就地执行此操作. 最小化操作总数. 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试. 02

leetcode || 143、Reorder List

problem: Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to {1,4,2,3}. Hide Tags Linked List 题意:对单链表重新排序,依次将尾

LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法

Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why?Suppose a sorted array is rotated at some pivot unknown to you be

[LeetCode] Search in Rotated Sorted Array I (33) &amp;&amp; II (81) 解题思路

33. Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise

Shiro源码分析之两种Session的方式

1.Shiro默认的Session处理方式 <!-- 定义 Shiro 主要业务对象 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <!-- <property name="sessionManager" ref="sessionManager" />

leetCode 74.Search a 2D Matrix(搜索二维矩阵) 解题思路和方法

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

leetCode 88.Merge Sorted Array (合并排序数组) 解题思路和方法

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements init

leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法

First Missing Positive Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 思路:这个题刚開始是没有思路的,难就难在O(n)时间内

Shiro源代码分析之两种Session的方式

1.Shiro默认的Session处理方式 <!-- 定义 Shiro 主要业务对象 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <!-- <property name="sessionManager" ref="sessionManager" />