题目描述
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?
我的解法
解题思路
将一个链表反转过来
实现代码
1 | /** |
Runtime: 8 ms, faster than 77.15% of C++ online submissions for Reverse Linked List.
Memory Usage: 9.3 MB, less than 77.10% of C++ online submissions for Reverse Linked List.
递归法
实现代码
1 | /** |
Runtime: 8 ms, faster than 77.11% of C++ online submissions for Reverse Linked List.
Memory Usage: 9.2 MB, less than 96.95% of C++ online submissions for Reverse Linked
代码分析
递归法感觉挺难理解的