連結串列反序,設有資料結構
typedef struct _list_node
{ double keyVal;
struct _list_node *next;
}ListNode;
ListNode* reverseList(ListNode* head)
{
ListNode *p1, *p2 , *p3;
//連結串列為空,或是單結點連結串列直接返回頭結點
if (head == NULL || head->next == NULL)
return head;
}
p1 = head;
p2 = head->next;
while (p2 != NULL)
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
head->next = NULL;
head = p1;
連結串列反序,設有資料結構
typedef struct _list_node
{ double keyVal;
struct _list_node *next;
}ListNode;
ListNode* reverseList(ListNode* head)
{
ListNode *p1, *p2 , *p3;
//連結串列為空,或是單結點連結串列直接返回頭結點
if (head == NULL || head->next == NULL)
{
return head;
}
p1 = head;
p2 = head->next;
while (p2 != NULL)
{
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
}
head->next = NULL;
head = p1;
return head;
}