来源:力扣 (LeetCode)
链接:https://leetcode-cn.com/problems/middle-of-the-linked-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
一、题目描述
给定一个带有头结点 head 的非空单链表,返回链表的中间结点。
如果有两个中间结点,则返回第二个中间结点。
示例 1:
- 输入:[1,2,3,4,5]
- 输出:此列表中的结点 3 (序列化形式:[3,4,5])
- 返回的结点值为 3 。 (测评系统对该结点序列化表述是 [3,4,5]) 。
注意,我们返回了一个 ListNode 类型的对象 ans,这样:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL
示例 2:
- 输入:[1,2,3,4,5,6]
- 输出:此列表中的结点 4 (序列化形式:[4,5,6])
- 由于该列表有两个中间结点,值分别为 3 和 4,我们返回第二个结点。
提示:
给定链表的结点数介于 1 和 100 之间。
二、题解
使用快慢指针,快指针每次走 2 步,慢指针每次走 1 步。当快指针指向 null 的时候慢指针就是中间节点。
问题的一个关键点是:如果有两个中间节点,返回第二个。因此要当快指针的下一个节点指向 null 时结束,否则结束条件是快指针的下一个节点是 null 。
三、代码
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
class Solution { public: ListNode *middleNode(ListNode *head) { ListNode *fast, *slow; // 链表为空或者只有一个节点 if (head == nullptr || head->next == nullptr) { return head; } // 快慢指针初始化 fast = head->next; slow = head; while (fast != nullptr) { // 快慢指针分别走一步 fast = fast->next; slow = slow->next; // 快指针在没有到达末尾的时候再走一步 if (fast) { fast = fast->next; } } return slow; } }; |
测试案例:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
TEST(leetcode, demo) { int i; Solution s; ListNode *nodes[6]; for (i = 0; i < 6; i++) { nodes[i] = new ListNode(i); } // 测试空链表 EXPECT_EQ(nullptr, s.middleNode(nullptr)); // 测试只有一个节点的链表 EXPECT_EQ(nodes[5], s.middleNode(nodes[5])); // 测试有两个节点的链表 EXPECT_EQ(nodes[4], s.middleNode(nodes[4])); // 测试 n 个节点的链表,n 为奇数 EXPECT_EQ(nodes[1], s.middleNode(nodes[1])); // 测试 n 个节点的链表,n 为偶数 EXPECT_EQ(nodes[0], s.middleNode(nodes[0])); } int main() { ::testing::InitGoogleTest(); return RUN_ALL_TESTS(); } |
![【每日打卡】[leetcode]876-链表的中间节点](https://www.dyxmq.cn/wp-content/uploads/2020/03/64d0d-imagece778ab033c9d90d.png)
![【深搜入门题】[leetcode]46-全排列](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/efdc7-image64636fa9d8634cc4.png&w=280&h=210&a=&zc=1)
![[leetcode]226-翻转二叉树](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/02/cff55-image8d13f59af4ceb4d0.png&w=280&h=210&a=&zc=1)
![[leetcode]300-最长上升子序列](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/02/968a3-image249c443b5e2b19e9.png&w=280&h=210&a=&zc=1)


![[leetcode]199-二叉树的右视图](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/f24a8-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]72-编辑距离](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/88c60-imageff84a6c5047db6ed.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]460-LFU缓存](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/5b3f2-image.png&w=280&h=210&a=&zc=1)
![[leetcode]125-验证回文串](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/5bfa5-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[程序员面试宝典]17.16-按摩师](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/9ecc9-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[剑指offer]面试题40-最小的k个数](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/c83c9-imagec745c425c670e18c.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]-409最长回文子串](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/e5589-image788d511d7c1e839c.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]面试题1.6-字符串压缩](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/776bc-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]695-岛屿的最大面积](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/aa889-image036941cd6bbf2296.png&w=280&h=210&a=&zc=1)
评论