int fact(int n) { if (n < 2) return 1; else return (n * (fact(n-1))); }
class ListNode { public: // ... constructors, member functions ... size_t length(); private: int val; ListNode *next; // ... maybe other member variables ... };
size_t ListNode::length() { if (next == NULL) return 1; else return (1 + next->length()); }
size_t length(const ListNode* X) { if (X->next == NULL) return 1; else return (1 + length(X->next)); }(In fact, in C++ the implicit pointer to the object has a name --- this.)
size_t ListNode::length() { size_t i=1; for(ListNode *p=next; p!=NULL; p=p->next) i++; return i; }The iterative version is very similar to the recursive version. Observe that they both run in O(n) --- linear time. This is not a coincidence. The linked-list structure is also linear --- the items are stored in a straight line.
Thus far, we focused on linear data structures: arrays and linked lists. Now we begin to look at nonlinear data structures.
Two important facts about functions acting on nonlinear data structures: