struct ListNode {
int data;
struct ListNode *next;
};


void printList( struct ListNode *L )
{
struct ListNode *p = L;
while (p) {

cout<<"data="<<p->data<<endl;
p = p->next;
}

}

struct ListNode* initList()
{
struct ListNode *node=NULL;
node=(struct ListNode*)malloc(sizeof(struct ListNode));
memset(node,0,sizeof(struct ListNode));
node->data=0;
node->next=NULL;
return node;
}
int listPushback(struct ListNode*L,int addNum)
{
struct ListNode *temp=L;
struct ListNode *pre=L;
while(temp!=NULL)
{
pre=temp;
temp=temp->next;

}
struct ListNode *newNode=(struct ListNode*)malloc(sizeof(struct ListNode));
memset(newNode,0,sizeof(struct ListNode));
newNode->data=addNum;
newNode->next=NULL;
pre->next=newNode;

return 0;
}

int listPopBack(struct ListNode*L)
{
if(L==NULL)
return -1;
struct ListNode *temp=L->next;
struct ListNode *pre=L;
while(temp!=NULL)
{

if(temp->next==NULL)
{
pre->next=temp->next;
int x=temp->data;
free(temp);
temp=NULL;

break;
}
pre=temp;
temp=temp->next;

}
return 0;
}

//首节点还没做处理(已处理)
int listDel(struct ListNode*L,int index)
{
if(L==NULL || (index<0))
return -1;
struct ListNode *temp=L;
struct ListNode *head=L;
int i=0;

if(index==0)
{
temp=temp->next;
while(temp!=NULL)
{
head->data=temp->data;
temp=temp->next;
head=head->next;
}

listPopBack(L);
return 0;
}

while(temp!=NULL)
{

if(i==index)
{
head->next=temp->next;
free(temp);
temp=NULL;
return 0;
}
head=temp;
temp=temp->next;
i++;

}

return 0;
}

//返回新链表
struct ListNode* listCopy(struct ListNode*L)
{
if(L==NULL)
return NULL;
struct ListNode *newNode=initList();
struct ListNode* pTemp=L->next;
while(pTemp!=NULL)
{
listPushback(newNode,pTemp->data);
pTemp=pTemp->next;
}
return newNode;

}
int listSize(struct ListNode*L)
{
int i=0;
struct ListNode* pTemp=L;

while(pTemp!=NULL)
{
i++;
pTemp=pTemp->next;
}
return i;
}
struct ListNode* listReverse(struct ListNode*L)
{
if (L == NULL || L->next == NULL) {
return L;
}
else
{
struct ListNode*beg=NULL;
struct ListNode*mid=L;
struct ListNode*end=L->next;
while(1)
{
//修改 mid 所指节点的指向
mid->next = beg;
//此时判断 end 是否为 NULL,如果成立则退出循环
if (end == NULL) {
break;
}
//整体向后移动 3 个指针
beg = mid;
mid = end;
end = end->next;

}
//最后修改 head 头指针的指向
L = mid;
return L;

}
return NULL;

}

int listUpdate(struct ListNode *L,int pos,int NewNum)
{
if(pos>=listSize(L))
return -1;
else
{
int i=0;
struct ListNode*pTemp=L;
while(1)
{
if(i==pos)
{
pTemp->data=NewNum;
break;
}
pTemp=pTemp->next;
}
}
return 0;
}
void freeList(struct ListNode *L)
{
while(L!=NULL)
{
struct ListNode *temp=L;
L=L->next;
free(temp);
cout<<"释放节点一次"<<endl;
}
}
int main()
{
int array[]={1,4,6,3,7,3};


vector<int>nums;
for(int i=0;i<sizeof(array)/sizeof(int);i++)
{
nums.push_back(array[i]);
}

struct ListNode* nodeList=initList();
listPushback(nodeList,1);
// listPushback(nodeList,2);
// listPushback(nodeList,3);
// listPushback(nodeList,4);

cout<<listSize(nodeList)<<endl;
printList(nodeList);

struct ListNode *newList=listReverse(nodeList);
cout<<"翻转后的链表:"<<endl;
printList(newList);



freeList(newList);


return 0;
}