java算法实现链表反转

 

public static Link ReverseLink1(Link head) {
    Link curr = head.Next;
    Link next = null;
    Link nextnext = null;
    //if no elements or only one element exists
    if (curr == null || curr.Next == null)
    {
        return head;
    }
    //if more than one element
    while (curr.Next != null)
    {
        next = curr.Next;       //1
        nextnext = next.Next;   //2
        next.Next = head.Next; //3
        head.Next = next;       //4
        curr.Next = nextnext;   //5
    }
    return head; }

点赞