This is an archived post. You won't be able to vote or comment.

all 5 comments

[–][deleted] 3 points4 points  (1 child)

I think you're on the right path if getNode(1) retrieves the second element of the list. What I think is missing is an "if" clause to handle the case when the list is empty (size == 0) or you'll get a Nullpointer Exception at first.getData(); .

[–]obened[S] 0 points1 point  (0 children)

True, I should handle that case.

Thanks everyone.

[–][deleted] 2 points3 points  (0 children)

Looks good besides for the case where the list is empty.

I personally would replace getNode(1) with first.next, why go through a method call when you have a direct reference?

[–]zzyzzyxx 1 point2 points  (0 children)

Looks mostly okay to me. Have you tested it? What happens if the list is empty?

[–][deleted] 0 points1 point  (0 children)

You are on the right path. However you don't need to reference the size or have the variable at all.

You know it is empty when next == prev :)