反转的方法
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head==null||head.next==null)
            return head;
        ListNode node=new ListNode(0);
        node.next=head;
        while(head.next!=null){
            ListNode temp=head.next;
            head.next=head.next.next;
            temp.next=node.next;
            node.next=temp;
        }
        
        return node.next;
         
    }
}
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head==null)
            return head;
        ListNode pre=head;
        ListNode cur=head.next;
        while(cur!=null){
            pre.next=cur.next;
            cur.next=head;
            head=cur;
            cur=pre.next;
        }
        return head;
         
    }
}
递归调用 
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode ReverseList(ListNode head) {
       if(head==null||head.next==null)
           return head;
        ListNode newHead=ReverseList(head.next);
        head.next.next=head;
        head.next=null;
        return newHead;
         
    }
}

 

 posted on 2019-07-24 18:15  JetaimeBeaucoup  阅读(102)  评论(0编辑  收藏  举报