Reverse Linked List

来源:互联网 发布:vc2015 windows 7 编辑:程序博客网 时间:2024/05/23 18:16

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 = NULL,*cur = head;        ListNode *nt;        while(cur){            nt = cur->next;            cur->next = pre;            pre = cur;            cur = nt;        }        return pre;    }};
0 0
原创粉丝点击