在函数中改变指针的问题。
程序代码:#include <iostream>
#include <queue>
using namespace std;
typedef struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}ListNode;
ListNode* f(ListNode *h2)
{
ListNode *h3=h2,*res=h2;
queue<ListNode *> q;
while(h2)
{
q.push(h2);
h2=h2->next;
}
while(!q.empty())
{
ListNode *t=q.front();
if(t->val==4)//当值为4时改变链表的结构,不把值为四的结点接入到链表中
{
q.pop();
continue;
}
h3=t;
h3=h3->next;
q.pop();
}
return res;//这里不应该是返回一个新的不含有四结点的链表吗?
}
int main()
{
ListNode *head=new ListNode(1);
ListNode *h=head,*h1=head;
h->next=new ListNode(2);
h=h->next;
h->next=new ListNode(3);
h=h->next;
h->next=new ListNode(4);
h=h->next;
h->next=new ListNode(5);
ListNode *t=f(head);
while(t)
{
cout<<t->val<<endl;
t=t->next;
}
return 0;
}








