思想:快慢指针
typedef struct ListNode node;
struct ListNode* middleNode(struct ListNode* head) {
node* fast = head;
node* slow = head;
while( fast && fast->next)
{
fast = fast->next->next;
slow = slow->next;
}
return slow;
}
第二个题:移除链表元素
truct ListNode* test(struct ListNode* pos,struct ListNode* head)
{
pos->next = head;
return head;
}
struct ListNode* removeElements(struct ListNode* head, int val) {
struct ListNode* phead = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* pos = phead;
while( head )
{
if( head->val != val)
{
pos = test(pos,head);
}
head = head->next;
}
pos->next = NULL;
return phead->next;
}