链表属于数据结构的问题,而单链表的倒置问题也是经常会出现在C/C++的面试当中,今天我们就来讲一下链表的倒置问题。
- 方法如下所示:
注意:传入的形参head为链表的头结点
typedef struct List
{
int a;
struct List *next;
}MyList;
MyList* InverseList(MyList* head)
{
if(null == head)
{
return null;
}
MyList *first = null, *second = null;
while(head)
{
second = head->next;
head->next = first;
first = head;
head = second;
}
return head;
}
- 示例图解: