Guest User

Untitled

a guest
Aug 10th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct ListNode {
  4.     int val;
  5.     ListNode* next;
  6. };
  7.  
  8. ListNode* middleNode(ListNode* head) {
  9.     ListNode *slow = head, *fast = head;
  10.     while (fast && fast->next) {
  11.         slow = slow->next;
  12.         fast = fast->next->next;
  13.     }
  14.     return slow;
  15. }
  16.  
  17. ListNode* arr_to_list(const std::vector<int>& arr) {
  18.     ListNode* head = nullptr;
  19.     for (auto it = arr.rbegin(); it != arr.rend(); ++it) {
  20.         head = new ListNode{*it, head};
  21.     }
  22.     return head;
  23. }
  24.  
  25. int main() {
  26.     auto node = arr_to_list({1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
  27.     std::cout << "Middle node is " << middleNode(node)->val << std::endl;
  28.     return 0;
  29. }
Add Comment
Please, Sign In to add comment