leetcode876.链表的中间结点 快慢指针

#include <stdio.h>
#include <algorithm>
#include<vector>
#include<set>
#include<iostream>
using namespace std;


struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

void out_list(ListNode *list){
    while(list!= nullptr){
        cout<< list->val;
        list =list->next;
    }
}
class Solution {
public:
    ListNode* middleNode(ListNode* head) {
        ListNode *slow , *fast;
        while(fast!=nullptr && fast->next != nullptr){
            fast = fast->next->next;
            slow = slow->next;
        }
        return slow;
    }

    ListNode*  reverse_list(ListNode* l1){
        ListNode *pre = l1; 
        ListNode *cur = l1->next;
        pre->next = nullptr;
        while(cur!=nullptr){// cur一直往后走,直到nullptr
            ListNode *next = cur->next;
            cur->next = pre;
            pre = cur;
            cur = next;
        }
        //out_list(pre);
        return pre;
    }
    
    ListNode* normal_add(ListNode* l1,ListNode* l2){
        int t=0;
        ListNode *head = new ListNode();
        ListNode* res = head;
        while(l1!=nullptr && l2!= nullptr){
            t =t+l1->val;
            l1 =l1->next;
            t =t+l2->val;
            l2 =l2->next;
            ListNode *p = new ListNode(t%10);
            head->next = p;
            head = p;
            t = t/10;
        }
        while(l1!=nullptr){
            t =t+l1->val;
            l1 =l1->next;
            ListNode *p = new ListNode(t%10);
            head->next = p;
            head = p;
            t = t/10;
        }
        while(l2!=nullptr){
            t =t+l2->val;
            l2 =l2->next;
            ListNode *p = new ListNode(t%10);
            head->next = p;
            head = p;
            t = t/10;
        }
        if(t==1){
            ListNode *p = new ListNode(t);
            head->next = p;
        }
        return res->next;
    }
};



int main()
{
    printf("Hello World");
    Solution *myslo = new Solution();
    vector<int> v1 = {7,2,4,3}; vector<int> v2 = {5,6,4}; //输出:[7,8,0,7]

    
    ListNode *p = new ListNode(v1[0]);
    ListNode *l1 = p;
    for(int i=1;i<v1.size();i++){
        ListNode *cur = new ListNode(v1[i]);
        p->next = cur;
        p = cur;
    }

    
    ListNode *res = myslo->middleNode(l1);
    cout<<"res_output"<<endl;
    cout<<endl<< res->val;
    out_list(res);
    delete myslo;
    return 0;
}