面试中程序测试,单链表的操作应该是比较容易出现的。单链表的一般操作,比如创建、遍历、插入、删除、清空等常规操作应该是必须掌握的操作,那么程序员面试中可能会出现“非常规”的链表操作,比如链表逆置。
由分析可知,长度为N的链表逆置需循环N次,每次循环将当前结点的Next指针保存,改变当前结点的Next指针指向前一指针(在上一次循环中保存)。
程序参考链接:
CList* converseList(CList *head){ CList *conHead = head; CList *before = NULL; CList *after = NULL; while (conHead->next != NULL) { after = conHead->next; conHead->next = before; before = conHead; conHead = after; } return *conHead;}