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 ...
随机推荐
- Android ListView工作原理完全解析,带你从源码的角度彻底理解
版权声明:本文出自郭霖的博客,转载必须注明出处. 目录(?)[+] Adapter的作用 RecycleBin机制 第一次Layout 第二次Layout 滑动加载更多数据 转载请注明出处:h ...
- MySQL中SYSDATE()和NOW()函数的区别和联系
MySQL中有5个函数需要计算当前时间的值: NOW.返回时间,格式如:2012-09-23 06:48:28 CURDATE,返回时间的日期,格式如:2012-09-23 CURTIME,返回时间, ...
- easyui datagrid如何获取到每行的文本框
在return '<input type="text" name="txtCount" class="inputvalue"/> ...
- js 操作select和option常用代码整理
1.获取选中select的value和text,html代码如下: <select id="mySelect"> <option value="1&qu ...
- 用Latex写IEEE论文
如果在搞科研,想在IEEE上发表文章,那么就不得不用IEEE的论文模板,分为doc版本和Tex版本.Tex是高德纳·纳什所写,大牛级的任务,写过<计算机程序设计艺术>,曾经是美国最年轻的科 ...
- 2016年终总结--一个Python程序猿的跨界之旅
时间过得真快.感觉15年年终总结刚写完,16年就结束了.看了blog,16年就写了可怜的8篇,对我来说16年还算顺风顺水. 真正可能出乎意料的是年底我离开了呆了2年半的龙图游戏,临时放弃了用了3年半的 ...
- Orchard运用 - 特定主题添加独立代码文件
今天继续跟大家分享捣鼓Orchard的一些心得.其实有时一些问题或者Bugs还是蛮好解决的,主要看你采取哪种方式方法.比如有时我们为了扩展某些特性或功能,你可以搭建一个全新的模块来完成,如果临时的或简 ...
- Windows-设置系统服务不开机启动
设置为手动,则开机不会自动启动了
- 比较String.substring()、String.slice()、String.substr()的区别
String.substring().String.slice().String.substr()这三者都能从String字符串中截取一部分,那么它们在使用上有什么不同么? 一.slice() 方法提 ...
- Redis数据类型--List
Redis列表是简单的字符串列表,依照插入顺序排序. 你能够加入一个元素到列表的头部(左边)或者尾部(右边) LPUSH 命令插入一个新的元素到头部, RPUSH插入一个新元素到尾部. 当一个这两个操 ...