华为机试—逆序链表输出
2015-01-09 23:55
183 查看
题目描述:
将输入的一个单向链表,逆序后输出链表中的值。链表定义如下:
typedef struct tagListNode
{
int value;
struct tagListNode *next;
}ListNode;
要求实现函数:
void converse(ListNode **head);
【输入】head: 链表头节点,空间已经开辟好
【输出】head: 逆序后的链表头节点
【返回】无
【注意】只需要完成该函数功能算法,中间不需要有任何IO 的输入输出
测试结果:
将输入的一个单向链表,逆序后输出链表中的值。链表定义如下:
typedef struct tagListNode
{
int value;
struct tagListNode *next;
}ListNode;
要求实现函数:
void converse(ListNode **head);
【输入】head: 链表头节点,空间已经开辟好
【输出】head: 逆序后的链表头节点
【返回】无
【注意】只需要完成该函数功能算法,中间不需要有任何IO 的输入输出
#include<iostream> typedef struct tagListNode { int value; struct tagListNode *next; }ListNode; void print(ListNode **head) { ListNode *f=(*head)->next; while(f) { printf("%d\n",f->value); f=f->next; } } void converse(ListNode **head) { ListNode *f=(*head)->next; ListNode* pre=NULL; ListNode *s; while(f){ s=f->next; f->next=pre; pre=f; f=s; } (*head)->next=pre; } int main(int argc, char *argv[]) { ListNode *h=(ListNode*)malloc(sizeof(ListNode)); h->next=NULL; for(int i=0;i<10;++i) { ListNode *tmp=(ListNode*)malloc(sizeof(ListNode)); tmp->value=i; tmp->next=h->next; h->next=tmp; } print(&h); converse(&h); printf("\n"); print(&h); return 0; }
测试结果:
相关文章推荐
- [华为机试练习题]24.删除链表中的反复节点、剩余节点逆序输出
- 华为机试—逆序链表输出
- [华为机试练习题]24.删除链表中的重复节点、剩余节点逆序输出
- 华为机试—逆序链表输出
- 牛客网三道编程题(四)华为机试在线训练(三)将数字逆序输出、将n个字符串按照字典顺序排序
- 华为机试题:删除链表中的重复节点、剩余节点逆序输出
- 链表逆序输出
- 用单链表来实现逆序输出
- 剑指offer 输出链表/逆序输出
- 【华为机试】输入n个整数,输出其中最小的k个
- 华为招聘机试整理11:实现字符串间隔输出
- 华为OJ题目(十九):输出单链表倒数第K个节点
- 剑指offer例题5—逆序输出链表
- 华为机试 - 8bitBCD码输出
- C语言实现单链表逆序与逆序输出实例
- 华为机试——通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- 【华为oj】输出单向链表中倒数第k个结点
- 【华为OJ】【035-输出单向链表中倒数第k个结点】
- 删除链表中的重复节点、剩余节点逆序输出
- 输入k 实现链表每k项都逆序输出