ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

链表的中间结点

2022-02-27 23:34:49  阅读:176  来源: 互联网

标签:结点 ListNode val nullptr head next 链表 中间


876. 链表的中间结点

给定一个头结点为 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 之间。

解题思路:

采用快慢指针,慢指针走一步,快指针走两步,当快指针到达尾结点时,慢指针就是中间节点;

图一:链表节点数为奇数时,当fast->next == nullptr时,slow就是中间节点;

图二:链表节点数为偶数时,当fast->next->next == nullptr时,slow->next就是第二个中间节点;

 

 

 

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using namespace std;
 5 
 6 class ListNode {
 7 public:
 8     ListNode() : val(0), next(nullptr) {}
 9     ListNode(int x) : val(x), next(nullptr) {}
10     ListNode(int x, ListNode *next) : val(x), next(next) {}
11 
12 public:
13     int val;
14     ListNode *next;
15 };
16 
17 class Solution {
18 public:
19     ListNode* middleNode(ListNode* head) {
20         ListNode *slow = head;
21         ListNode *fast = head;
22         if (head == nullptr) {
23             return nullptr;
24         }
25         while (fast->next != nullptr && fast->next->next != nullptr) {
26             slow = slow->next;
27             fast = fast->next->next;
28         }
29         return (fast->next == nullptr) ? slow : slow->next;
30     }
31     void insertNodeAtTail(ListNode *&head, int val) {
32         ListNode *newNode = new ListNode(val);
33         ListNode *currentNode = head;
34         if (head == nullptr) {
35             head = newNode;
36             return;
37         }
38         while (currentNode->next != nullptr) {
39             currentNode = currentNode->next;
40         }
41         currentNode->next = newNode;
42         return;
43     }
44     void printfListNode(ListNode *head) {
45         while (head != nullptr) {
46             std::cout << head->val << "->";
47             head = head->next;
48         }
49         std::cout << "NULL" << endl;
50         return;
51     }
52 };
53 int main()
54 {
55     Solution *test = new Solution();
56     vector<int> vec = {1, 2, 3, 4, 5, 6, 7 };
57     ListNode *head = nullptr;
58     for (const auto &v : vec) {
59         test->insertNodeAtTail(head, v);
60     }
61     test->printfListNode(head); // 1->2->3->4->5->6->7->NULL
62 
63     ListNode *midNode = test->middleNode(head);
64     if (midNode != nullptr) {
65         std::cout << "midNode's value:" << midNode->val << endl; // 4
66     } else {
67         std::cout << "midNode's value is nullptr!" << endl;
68     }
69     test->insertNodeAtTail(head, 8); // 1->2->3->4->5->6->7->8->NULL
70     test->printfListNode(head);
71     midNode = test->middleNode(head);
72     if (midNode != nullptr) {
73         std::cout << "midNode's value:" << midNode->val << endl; // 5
74     } else {
75         std::cout << "midNode's value is nullptr!" << endl;
76     }
77     delete test;
78     system("pause");
79     return 0;
80 }

 

运行结果:

 

 

 

标签:结点,ListNode,val,nullptr,head,next,链表,中间
来源: https://www.cnblogs.com/MGFangel/p/15943928.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有