eolymp
bolt
Try our new interface for solving problems
Problems

LinkedList Sum

LinkedList Sum

Given a linked list, find the sum of its elements. Definition of a single linked list: \begin{lstlisting}[language=Java] // Java class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; } } \end{lstlisting} \begin{lstlisting}[language=C++] // C++ class ListNode { public: int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; \end{lstlisting} \begin{lstlisting}[language=C] // C struct ListNode { int val; struct ListNode *next; }; \end{lstlisting} Implement function \textbf{sum} that finds the sum of linked list elements. \begin{lstlisting}[language=Java] // Java int sum(ListNode head) \end{lstlisting} \begin{lstlisting}[language=C++] // C, C++ int sum(ListNode *head) \end{lstlisting} \Note \includegraphics{https://static.e-olymp.com/content/f3/f3bb2da52a69ad6747cf93b8ecf5e0cc95c92441.gif} The sum of elements of a linked list is $6$.
Time limit 1 second
Memory limit 128 MiB
Author Mykhailo Medvediev