206.Reverse-Linked-List
206. Reverse Linked List
题目地址
题目描述
Reverse a linked list.代码
Approach 1: 使用while Iterative
public class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
// prev => cur => next
while(curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
head = prev;
return head;
}
}Approach #2 Recursive
Last updated