思路:设置两个指针,first 和 second,first 每次走1步,second每次走两步
second走到最后一个节点时,取first即可
#include <iostream>
using namespace std;
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
/* 求链表的中间的那个元素 */
typedef linkNode{
int val;
linkNode* next;
}linkNode;
linkNode* findmiddle(linkNode* head){
linkNode *first = head,*second = head;
while(second->next){
first = first->next;
second = second->next;
if(second->next){
second = second->next;
}
}
return first;
}
/*奇数:n/2+1 偶数:n/2 */