206. Reverse Linked List
2015-12-21 01:10
330 查看
Reverse a singly linked list.
click to show more hints.
Subscribe to see which companies asked this question/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head)
{
if(!head)
return NULL;
if(!head->next)
return head;
ListNode* x=head;
ListNode* y=x->next;
x->next=NULL;
while(y->next)
{
ListNode*z=y->next;
y->next=x;
x=y;
y=z;
}
y->next=x;
head=y;
return head;
}
};
click to show more hints.
Subscribe to see which companies asked this question/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head)
{
if(!head)
return NULL;
if(!head->next)
return head;
ListNode* x=head;
ListNode* y=x->next;
x->next=NULL;
while(y->next)
{
ListNode*z=y->next;
y->next=x;
x=y;
y=z;
}
y->next=x;
head=y;
return head;
}
};
相关文章推荐
- Linux学习之:用户和文件权限管理
- 利用svd进行用户商品推荐的小实践
- 5.利用 NSNotificationCenter实现键盘弹出时页面自适应
- 1004. 成绩排名 (20)
- android微信的抢红包插件
- Chromium硬件加速渲染的UI合成过程分析
- javaday25-多线程下
- Leetcode: Shortest Word Distance
- leetcode-- Longest Common Prefix
- Swift 控制流(笔记)
- Emmagee - 简单的性能测试工具
- js函数解释(包括内嵌,对象等)
- 小米真机调试Android程序
- Divide and Conquer:River Hopscotch(POJ 3258)
- Spring AOP概念理解 (通俗易懂)
- 黑马程序员-----self和super关键字的介绍
- 【COCOS2DX-游戏开发之三十】抖动效果 CCShake
- 黑马程序员-----多态的概念和条件
- 期末总结
- 黑马程序员-----类的继承和派生概念