您的位置:首页 > 编程语言 > Go语言

leetcode: (206) Reverse Linked List

2015-09-08 22:44 597 查看
【Question】

Reverse a singly linked list.

借助一个辅助指针来保存当前指针的后一个节点,遍历一次即可

/**
* 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) {
ListNode *pre,*cur,*nex;
if (head==NULL) return head;
pre=head;
cur=head->next;
while(cur)
{
nex=cur->next;
cur->next=pre;
pre=cur;
cur=nex;
}
head->next=NULL;
head=pre;
return head;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode algorithms 链表