`

单项链表 反转

阅读更多

public class Node {
    Object value;
    Node next;

    public Node(Object value) {
        this.value = value;
    }
}

void reverse(Node head) {
    Node previous = null;
    if (head == null) {
        return;
    }
    while (head != null) {
        Node nextNode = head.next;
        head.next = previous;
        previous = head;
        head = nextNode;
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics