Atharv Kurdukar
Atharv Kurdukar

Reputation: 135

Recursive Logic for Linked List in Java

I am trying to find a way to write a java program with recursion logic for insertion, searching as well as traversal for singly linked list. But, I don't know how I can do it while my head node is private. Here is the piece of code I have written :

class Node {
    int data;
    Node next;
}
public class SingleList {
    private Node head;

    public SingleList() {
        head = null;
    }
    void insert(Node temp, int num, int n) {
        //Suggest some code here
    }

    boolean search(Node temp, int num) {
        //Suggest some code here
    }
    void traverse(Node temp) {
        //Suggest some code here
    }
}

Upvotes: 0

Views: 114

Answers (1)

Roni Koren Kurtberg
Roni Koren Kurtberg

Reputation: 515

You don't have any problem to access the head from your class.

In addition, the Node class is probably irrelevant for any other class other than SingleList class, it will be a better idea to include it within the SingleList class

public class SingleList {

    private class Node {
        int data;
        Node next;
    }

    // Rest of your class code...

}

Upvotes: 0

Related Questions