无法播放?请 点击这里 跳转到Youtube
切换视频源:
题目: https://leetcode.com/problems/reverse-linked-list/
Iterative
class Solution {
public ListNode reverseList(ListNode head) {
ListNode res = null;
while (head != null) {
ListNode next = head.next;
head.next = res;
res = head;
head = next;
}
return res;
}
}
Recursive
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode reversed = reverseList(head.next);
head.next.next = head;
head.next = null;
return reversed;
}
}