0
点赞
收藏
分享

微信扫一扫

LeetCode-206. Reverse Linked List

九点韶留学 2022-08-10 阅读 25


Reverse a singly linked list.

Example:

Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

Follow up:

A linked list can be reversed either iteratively or recursively. Could you implement both?

题解:

class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *pre = NULL, *cur = head;
while (head != NULL) {
cur = head;
head = head->next;
cur->next = pre;
pre = cur;
}
return cur;
}
};

 


 

举报

相关推荐

0 条评论