466. Count Linked List Nodes【Naive】
Count how many nodes in a linked list.
Given 1->3->5
, return 3
.
解法一:
/**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/ class Solution {
public:
/*
* @param head: the first node of linked list.
* @return: An integer
*/
int countNodes(ListNode * head) {
if (head == NULL) {
return ;
} else if (head->next == NULL) {
return ;
} return + countNodes(head->next);
}
};
递归
解法二:
/**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/ class Solution {
public:
/*
* @param head: the first node of linked list.
* @return: An integer
*/
int countNodes(ListNode * head) {
if (head == NULL) {
return ;
} int sum = ;
while (head != NULL) {
++sum;
head = head->next;
} return sum;
}
};
466. Count Linked List Nodes【Naive】的更多相关文章
- 452. Remove Linked List Elements【Naive】
Remove all elements from a linked list of integers that have value val. Example Given 1->2->3- ...
- 92. Reverse Linked List II【Medium】
92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 141. Linked List Cycle【easy】
141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...
- 203. Remove Linked List Elements【Easy】【未排序链表删除其中的给定值】
Remove all elements from a linked list of integers that have value val. Example: Input: 1->2-> ...
- 219. Insert Node in Sorted Linked List【Naive】
Insert a node in a sorted linked list. Example Given list = 1->4->6->8 and val = 5. Return ...
- 102. Linked List Cycle【medium】
Given a linked list, determine if it has a cycle in it. Example Given -21->10->4->5, tail ...
- 141. Linked List Cycle【Easy】【判断链表是否存在环】
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- 366. Fibonacci【Naive】
Find the Nth number in Fibonacci sequence. A Fibonacci sequence is defined as follow: The first two ...
随机推荐
- (转)找回vss超级管理员密码
原文:http://www.cnblogs.com/446557021/archive/2011/01/05/1926213.html 如果忘记了VSS管理员密码,打开vss数据库所在的文件夹,打开d ...
- JVM 虚拟机字节码指令表
把JVM虚拟机字节指令表整理了一下,方便搜索,偶尔复习下 纯手工整理,可能存在一些问题,如果发现请及时告之我会修正 字节码 助记符 指令含义 0x00 nop None 0x01 aconst_nul ...
- http://www.cnblogs.com/zhoujinyi/p/3437475.html
http://www.cnblogs.com/zhoujinyi/p/3437475.html
- 通过Spring配置文件中bean中的property赋值
基本数据类型赋值-通过spring配置文件中bean中的property 扩展-以此方式可以通过配置为连接数据的属性赋值 1.如果是基本数据类型,可以通过setter方法为对象中的属性设置初始值,应用 ...
- vb.net中将DataGridView与数据源绑定
在< .net中将DataGridView内的数据导出为Excel表格>中说了如何导出数据到Excel,今天这篇文章将讲述如何绑定数据源,在控件中显示我们需要的信息. 在敲机房收费系统的时 ...
- libmysqld,嵌入式MySQLserver库
25.1.1. 嵌入式MySQLserver库概述 使用嵌入式MySQLserver库,可以在client应用程序中使用具备所有特性的MySQLserver. 主要长处在于.添加了速度.并使得嵌入式应 ...
- [ES6] 02. Traceur compiler and Grunt
Local Install: npm install -g traceur npm install grunt-contrib-watch npm install grunt-traceur-lat ...
- Tomcat 之 启动tomcat时 错误: 代理抛出异常 : java.rmi.server.ExportException: Port already in use: 1099;
错误: 代理抛出异常 : java.rmi.server.ExportException: Port already in use: 1099; nested exception is: java. ...
- vim 如何编辑 GB2312 编码的文件?
vim 如何编辑 GB2312 编码的文件? 彻底搞清楚字符编码: ASCII, ISO_8859, GB2312,UCS, Unicode, U 结合file和iconv命令转换文件的字符编码类型 ...
- python 网络请求类库 requests 使用
python 网络请求类库 requests 使用 requests是 为python封装的强大 REST 操作类库 githubhttps://github.com/kennethreitz/req ...