您的位置:首页 > 其它

【leetcode每日一题】NO206.Reverse Linked List

2015-08-13 10:13 387 查看
题目: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) {
        if(head==NULL)
            return head;
        ListNode *curNode=head,*nextNode=head->next,*temp;
        while(nextNode!=NULL)
        {
            temp=nextNode->next;
            nextNode->next=curNode;
            curNode=nextNode;
            nextNode=temp;
        }
        head->next=NULL;
        head=curNode;
        return head;
    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: