首页 > 其他 > 详细

leetcode No206. Reverse Linked List

时间:2016-08-09 17:29:31      阅读:216      评论:0      收藏:0      [点我收藏+]

Question:

Reverse a singly linked list.

翻转链表

Algorithm:

见程序

Accepted Code:

/**
 * 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||head->next==NULL)
            return head;
        ListNode* l1=NULL;
        ListNode* l2=head;
        while(l2)
        {
            ListNode* tmp=l2->next;
            l2->next=l1;
            l1=l2;
            l2=tmp;
        }
        return l1;
    }
};



leetcode No206. Reverse Linked List

原文:http://blog.csdn.net/u011391629/article/details/52164389

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!