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

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;
    }
}

 

posted @ 2016-11-21 10:51  破玉  阅读(106)  评论(0编辑  收藏  举报