Utopi
a

反转链表

Recursive!Recursive!and Recursive!

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?

Mine (0 ms)

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode reverseList(ListNode head) { if ( head == null || head.next == null ){ return head; } else { ListNode newHead = reverseList ( head.next ); head.next.next = head; head.next = null; return newHead; } } }
Screen Shot 2020-03-16 at 10.06.56 PM $$ by :https://www.jianshu.com/p/f7534f8d7bf2 $$

递归真是博大精深啊!!!

Standard Answer

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode reverseList(ListNode head) { ListNode prev = null; ListNode curr = head; while (curr != null) { ListNode nextTemp = curr.next; curr.next = prev; prev = curr; curr = nextTemp; } return prev; } }

哇_(:з」∠)_车轮战啊这是!!!但是好理解多了。