程序填空题:单链表逆转(不带有头节点)
以下这段程序将单链表逆转。(单链表不带有空头结点,链表头指针是`head`)
例如,链表 1 -> 2 -> 3 -> 4 逆转后变为 4 -> 3 -> 2 -> 1 .
```c
#include
#include
struct Node{
int data;
struct Node* next;
};
struct Node* createList(){
/* 细节省略 */
}
void reverseList( @@[struct Node** phead](2) ){
struct Node* tmp, *newHead, *oldHead;
newHead = NULL;
oldHead = @@[*phead](2) ;
while(oldHead){
tmp = oldHead;
oldHead = @@[oldHead->next](2) ;
tmp->next = @@[newHead](2) ;
newHead = @@[tmp](2) ;
}
*phead = newHead;
}
int main(){
struct Node* head=createList();
reverseList( @@[&head](2) );
return 0;
}
```
答案:
第1空:struct Node** phead
第2空:*phead
第3空:oldHead->next
第4空:newHead
第5空:tmp
第6空:&head
例如,链表 1 -> 2 -> 3 -> 4 逆转后变为 4 -> 3 -> 2 -> 1 .
```c
#include
#include
struct Node{
int data;
struct Node* next;
};
struct Node* createList(){
/* 细节省略 */
}
void reverseList( @@[struct Node** phead](2) ){
struct Node* tmp, *newHead, *oldHead;
newHead = NULL;
oldHead = @@[*phead](2) ;
while(oldHead){
tmp = oldHead;
oldHead = @@[oldHead->next](2) ;
tmp->next = @@[newHead](2) ;
newHead = @@[tmp](2) ;
}
*phead = newHead;
}
int main(){
struct Node* head=createList();
reverseList( @@[&head](2) );
return 0;
}
```
答案:
第1空:struct Node** phead
第2空:*phead
第3空:oldHead->next
第4空:newHead
第5空:tmp
第6空:&head