data structures - How to find the last element in the singly linked list? -


in singly linked list, know next of last node point null, can find out traversing.

if last node of singly linked list point middle node how can find last node?

if "last node" points other node, isn't last node, it? not mention stretch , possibly break commonly accepted definiton of "list".

normally find last element

node *current = list.start,      *next = current.next;  while (next != null) {     current = next;     next = current.next; }  print("last node " + current->value); 

however, assumes "last node" point null. otherwise stuck in infinite loop.

it's practice keep pointer last node of list first, that's trivial solution doesn't depend on last node pointing null.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -