swap every second node?# Programming - 葵花宝典
s*e
1 楼
I give you a singly linked Linked List. Write a function to swap every second
node. [ie - 1->2->3->4->5->| becomes 2->1->4->3->5->|]
大家有没有更好的办法?
BOOL SwapList(node** pphead)
{
if(*pphead == NULL)
return FALSE;
// two pointers
node* pNode1 = *pphead;
node* pNode2 = (*pphead)->pNext;
*pphead = pNode2;
while(pNode2->pNext != NULL)
{
// two temporay pointers
node* pTmp1 = pNode1->pNext->pNext;
node* pTmp2 = pNode2->pNext->pNext;
node. [ie - 1->2->3->4->5->| becomes 2->1->4->3->5->|]
大家有没有更好的办法?
BOOL SwapList(node** pphead)
{
if(*pphead == NULL)
return FALSE;
// two pointers
node* pNode1 = *pphead;
node* pNode2 = (*pphead)->pNext;
*pphead = pNode2;
while(pNode2->pNext != NULL)
{
// two temporay pointers
node* pTmp1 = pNode1->pNext->pNext;
node* pTmp2 = pNode2->pNext->pNext;