Line List, preend node

I have a problem understanding the exercises. I have to develop a linear linked list. But I do not need to distinguish between the list and node.

The Node constructor should create a Node and add it to the list, which is passed as a parameter.

I usually looked at the list and added Node at the end. Here is my code.

class Node{
    Object data;
    Node link;

    public Node(Object pData, Node pLink){
        this.data = pData;
        this.link = pLink;
    }

    public String toString(){
        if(this.link != null){
            return this.data.toString() + this.link.toString();
        }else{
            return this.data.toString() ;
        }
    }

    public void inc(){
        this.data = new Integer((Integer)this.data + 1);
    }
}

Perhaps I just learned a lot today, and my brain cannot accept more information: D, please help!

+3
source share
2 answers

You need to change the following node pointer to point to a list that is passed as a parameter.

This is what your code is already doing. I tried running it and gives the correct result. :)

, toString, , 9.

+1

, , , , , .

,

Node head = ...

,

head = new Node(..., head)

, , node.

+1

Source: https://habr.com/ru/post/1785286/


All Articles