Reputation: 325
i have a question about circularly linked lists. My linked list object has two references, first
and last
, and the next node of the last
reference is first
. I want to write a method that inserts a node into the end of the list.
void insertLast(int k) {
Node a = new Node(k);
if (first == null) {
first = last = a;
} else {
last.after = a;
a.after = first;
}
last = a
}
Is something like this possible? Have I made a mistake?
Upvotes: 3
Views: 1190
Reputation: 597362
Yes, it is.
last.setNext(newNode)
)newNode.setNext(first)
)last = newNode
)Upvotes: 1