LeetCode 206. Reverse Linked List
2016-03-09 01:57
381 查看
Reverse a singly linked list.
click to show more hints.
Hint:
A linked list can be reversed either iteratively or recursively. Could you implement both?
一,算法分析
头插法重建单链表,注意默认链表是没有头结点的,所以建表的时候不断更新head节点;Hint中的提示应该有其他更效率办法,今天就先不看了,第二遍再看吧
二,C语言实现
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseList(struct ListNode* head) {//同样是不带头结点
struct ListNode *p,*q;
if(head==NULL || head->next==NULL)
return head;
p=head->next;
head->next=NULL;
while(p!=NULL){
q=p->next;
p->next=head;
head=p;
p=q;
}
return head;
}
click to show more hints.
Hint:
A linked list can be reversed either iteratively or recursively. Could you implement both?
一,算法分析
头插法重建单链表,注意默认链表是没有头结点的,所以建表的时候不断更新head节点;Hint中的提示应该有其他更效率办法,今天就先不看了,第二遍再看吧
二,C语言实现
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseList(struct ListNode* head) {//同样是不带头结点
struct ListNode *p,*q;
if(head==NULL || head->next==NULL)
return head;
p=head->next;
head->next=NULL;
while(p!=NULL){
q=p->next;
p->next=head;
head=p;
p=q;
}
return head;
}
相关文章推荐
- 如何组织构建多文件 C 语言程序(二)
- 如何写好 C main 函数
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- Lua和C语言的交互详解
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- C#定义并实现单链表实例解析
- C#数据结构之单链表(LinkList)实例详解
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解