您的位置:首页 > 其它

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;

}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: