LeetCode 206 Reverse Linked List

While loop 做法:

 1 class Solution {
 2     public ListNode reverseList(ListNode head) {
 3         ListNode prev = null, cur = head;
 4         while (cur != null) {
 5             ListNode node = cur.next;
 6             cur.next = prev;
 7             prev = cur;
 8             cur = node;
 9         }
10         return prev;
11     }
12 }

Recursion 做法:

 1 class Solution {
 2     public ListNode reverseList(ListNode head) {
 3         if (head == null || head.next == null) {
 4             return head;
 5         }
 6         ListNode newHead = reverseList(head.next);
 7         ListNode tail = head.next;
 8         tail.next = head;
 9         head.next = null;
10         return newHead;
11     }
12 }
原文地址:https://www.cnblogs.com/mayinmiao/p/8488189.html