链接列表撤消

时间:2016-06-22 12:41:52

标签: java linked-list

我的链接列表反转代码无法编译。一切似乎在逻辑上都是正确的。

以下是我的LinkedList课程的片段:

public class LinkedList {
    Node head;

    public static class Node {
        int  data;
        Node next;

        Node(int d) {
            this.data = d;
            this.next = null;
        }
    }

    public void display(Node n) {
        n = head;
        int ctr;

        while (n != null) {
            System.out.print(n.data +" ");
            n = n.next;
        }

        ctr = countNodes(head);
        System.out.println("The list length is "+ctr);
    }

    public Node recreverse(Node n){
        Node prev = null;
        Node temp = null;

        if (n.next == null) {
            head = n;
            head.next = prev;
            return head;
        } else {
            temp = n;
            n = n.next;

            prev = n;
            prev.next = temp;

            return n;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

countNodes(Node)未在任何地方定义:

ctr = countNodes(head);

定义它并编译代码。