How to find nth element from the end of a singly linked list?

后端 未结 28 1110
感动是毒
感动是毒 2020-12-04 06:08

The following function is trying to find the nth to last element of a singly linked list.

For example:

If the elements are

28条回答
  •  情歌与酒
    2020-12-04 06:42

    What do you think regarding this approach.

    1. Count length of the linkedlist.
    2. Actual Node index from head = linkedlist length - given index;
    3. Write a function to travesre from head and get the node at the above index.

提交回复
热议问题