题解 | #反转链表# 2种解法:双指针、递归
反转链表
http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
双指针
public class Solution { public ListNode ReverseList(ListNode head) { if (head == null) { return null; } else { ListNode next = null; ListNode pre = null; while (head != null) { next = head.next; head.next = pre; pre = head; head = next; } return pre; } } }
递归
public class Solution { public ListNode ReverseList(ListNode head) { if (head == null || head.next == null) return head; ListNode last = ReverseList(head.next); head.next.next = head; head.next = null; return last; } }