首页 笔记 图片 查字 英语
所属分类:其它
浏览:28
内容:

代码:

// 反转链表迭代法
public class ReverseList {

    public static void main(String[] args) {
        Node3 node1 = new Node3(1);
        Node3 node2 = new Node3(2);
        Node3 node3 = new Node3(3);
        Node3 node4 = new Node3(4);
        Node3 node5 = new Node3(5);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = null;

        Node3 iterate = iterate(node1);
        System.out.println(iterate);
    }

    static Node3 iterate(Node3 head) {
        Node3 prev = null;
        Node3 next;
        Node3 curr = head;

        while (curr != null) {
            next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }
}