8.单链表实现与反转

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/guchunchao/article/details/82814652
public class Node {
    int index;
    Node next;

    public Node(int index, Node next) {
      this.index = index;
      this.next = next;
    }

    /**
     *先找到最后一个节点,然后从最后一个节点之前的那个节点的方法体中开始将下一个指向当前一个,
     *然后当前节点反转时其后面的节点已经进行反转了,不需要管。最后返回原来的最后一个节点。
     */
    public static Node reverse3(Node node) {
	    if(node.next==null)return node;
	    Node next = node.next;
	    node.next = null;
	    Node re = reverse3(next);
	    next.next = node;
	    return re;
	}
}

猜你喜欢

转载自blog.csdn.net/guchunchao/article/details/82814652