首页 > C&&C++, Programming > 链表

链表

1. 逆转链表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
typedef struct Node
{
   int Data;
   Node *Next;
};

Node *ReverseList(Node *pHead)
{
   Node *pReverseHead = NULL;
   while (pHead != NULL)
   {
      Node *pTmp = pHead;
      pHead = pHead->Next;
      pTmp->Next = pReverseHead;
      pReverseHead = pTmp;
   }
   return pReverseHead;
}

bool hasCircle(Node *pHead)
{
   Node *pSlow = pHead;
   Node *pFast = pHead;
   while (pSlow && pFast && (pFast=pFast->Next) && pFast!=pSlow)
   {
      pSlow=pSlow->Next;
      pFast=pFast->Next;
   }
   return pFast != NULL;
}
分类: C&&C++, Programming 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.