博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
lintcode 466. 链表节点计数
阅读量:4484 次
发布时间:2019-06-08

本文共 695 字,大约阅读时间需要 2 分钟。

计算链表中有多少个节点.

样例

给出 1->3->5, 返回 3.

1 /** 2  * Definition of ListNode 3  * class ListNode { 4  * public: 5  *     int val; 6  *     ListNode *next; 7  *     ListNode(int val) { 8  *         this->val = val; 9  *         this->next = NULL;10  *     }11  * }12  */13 14 15 class Solution {16 public:17     /*18      * @param head: the first node of linked list.19      * @return: An integer20      */21     int countNodes(ListNode * head) {22         // write your code here23         int count = 0;24         while (head->next) {25             count++;26             head = head->next;27         }28         return count;29     }30 };

 

转载于:https://www.cnblogs.com/gousheng/p/7904648.html

你可能感兴趣的文章
Sql Server 判断表或数据库是否存在
查看>>
计算机网络
查看>>
iOS-浅谈runtime运行时机制
查看>>
数字证书原理 - 转自 http://www.cnblogs.com/JeffreySun/archive/2010/06/24/1627247.html
查看>>
关于float和margin
查看>>
Python练习-内置函数的应用
查看>>
洛谷P3905 道路重建
查看>>
数据表格 - DataGrid - 行编辑
查看>>
HQL查询语句
查看>>
jsp听课笔记(四)
查看>>
vim
查看>>
数组的键/值操作函数
查看>>
Android单点触控与多点触控切换的问题解决方案
查看>>
JS常用函数与方法
查看>>
十、Shell基础
查看>>
py16 面向对象深入
查看>>
CentOS 7 安装 Gitlab
查看>>
JavaScript-03-常见函数
查看>>
ajax 设置Access-Control-Allow-Origin实现跨域访问
查看>>
去掉ExpandableListView的箭头图标
查看>>