每天一道LeetCode--206. Reverse Linked List

破玉發表於2016-11-21

Reverse a singly linked list.

package cn.magicdu;

import cn.magicdu.extra.ListNode;

public class _206_Reverse_Linked_List {
    /**
     * 迭代
     * @param head
     * @return
     */
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null)
            return head;
        ListNode l = new ListNode(0);
        l.next = head;
        ListNode pre = head;
        ListNode p = pre.next;
        while (p != null) {
            pre.next = p.next;
            p.next = l.next;
            l.next = p;
            p = pre.next;
        }
        return l.next;
    }
    
    
    /**
     * 遞迴
     * @param head
     * @return
     */
    public ListNode reverseList1(ListNode head) {
        if(head==null || head.next==null)
            return head;
        ListNode nextNode=head.next;
        ListNode newHead=reverseList(nextNode);
        nextNode.next=head;
        head.next=null;
        return newHead;
    }
}

 

相關文章