Reverse Linked List
Reverse a singly linked list.
(翻转链表)
Note:
A linked list can be reversed either iteratively or recursively. Could you implement both?
Example:
1. 迭代法
构建一个头结点,在遍历链表的过程中不断地采用头插法即可翻转链表。具体实现过程如下:
1 | # Definition for singly-linked list. |
2. 递归法
递归的翻转后面的字符串,然后将当前的头结点放在最尾部,具体实现过程如下:
1 | # Definition for singly-linked list. |