Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- struct ListNode {
- int val;
- ListNode* next;
- };
- ListNode* middleNode(ListNode* head) {
- ListNode *slow = head, *fast = head;
- while (fast && fast->next) {
- slow = slow->next;
- fast = fast->next->next;
- }
- return slow;
- }
- ListNode* arr_to_list(const std::vector<int>& arr) {
- ListNode* head = nullptr;
- for (auto it = arr.rbegin(); it != arr.rend(); ++it) {
- head = new ListNode{*it, head};
- }
- return head;
- }
- int main() {
- auto node = arr_to_list({1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
- std::cout << "Middle node is " << middleNode(node)->val << std::endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment