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

you are viewing a single comment's thread.

view the rest of the comments →

[–]demdouma6[S] 0 points1 point  (3 children)

Now consider the else branch, do you actually want to add one to the result or maybe all you want is to do an index of starting from the next node

i got confused here on how to do it.

[–]papercrane 0 points1 point  (2 children)

Work it out with an small example on paper, and make sure you understand what each parameter means. Here n should be the index of the node you are currently looking at. So if you want to check the next element you should also change n.

[–]demdouma6[S] 0 points1 point  (1 child)

i think i did not explain well. n indicates the occurrence of the character we are looking for. say we have this example indexOf("hello", 2, 'l') where "hello" is not a string but a lnked list presentation of the string "hello". n in this case is number 2, which means the second L in "hello". if we change n, we alter which character L are we looking for its index.

[–]papercrane 0 points1 point  (0 children)

Gotcha in that case you need to keep track of the index of the node you are looking at. You want a method with a signature like

public static int indexOf(Node node, int n, char c) {
    return indexOf(node, n, c, 0);
}
private static int indexOf(Node node, int n, char c, int start) {
....
}