Count how many nodes in a linked list.

Example

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】的更多相关文章

  1. 452. Remove Linked List Elements【Naive】

    Remove all elements from a linked list of integers that have value val. Example Given 1->2->3- ...

  2. 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 ...

  3. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  4. 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 ...

  5. 203. Remove Linked List Elements【Easy】【未排序链表删除其中的给定值】

    Remove all elements from a linked list of integers that have value val. Example: Input: 1->2-> ...

  6. 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  ...

  7. 102. Linked List Cycle【medium】

    Given a linked list, determine if it has a cycle in it.   Example Given -21->10->4->5, tail ...

  8. 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 ...

  9. 366. Fibonacci【Naive】

    Find the Nth number in Fibonacci sequence. A Fibonacci sequence is defined as follow: The first two ...

随机推荐

  1. EventBus (一) 使用详解——初步使用EventBus

    版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 前言:EventBus是上周项目中用到的,网上的文章大都一样,或者过时,有用的没几篇,经过琢磨,请教他人,也终于弄清楚点眉目,记 ...

  2. django的过滤和搜索排序功能django-filter

    参考: 1.https://django-filter.readthedocs.io/en/master/guide/usage.html#the-filter 2.https://www.cnblo ...

  3. Python操作dict时避免出现KeyError的几种方法

    见原文:https://www.polarxiong.com/archives/Python-%E6%93%8D%E4%BD%9Cdict%E6%97%B6%E9%81%BF%E5%85%8D%E5% ...

  4. 在线分享Oracle尖峰时刻--2014年中秋节尖峰在线福利!

    **********************************************************            2014年中秋节尖峰在线福利!*************** ...

  5. ZentaoPMS 系统的优先级以及修改

    王颖2015/10/08 最新版本中,严重程度1.2.3.4分别代表什么意思?还有优先级中1.2.3.4分别代表什么意思? 回复 石洋洋2015/10/08 1 2 3 4 都代表不同的级别,一般是1 ...

  6. Postgresql死锁的处理

    今天遇到一个奇怪的现象,select和delete表时正常执行,但truncate和drop表时会一直运行,也不报错. 查了些资料才发现问题的原因,总结如下: "drop table &qu ...

  7. Django安装和配置环境变量

    一.windows系统安装Django 1.先安装python2.x or 3.x软件.(记得勾选pip3和添加python自己的环境变量) 下载地址:http://www.python.org/ 2 ...

  8. Chrome 制作绿色便携版

    1.建立一个新的文件夹命名为Chrome 2.将电脑上默认的Chrome文件复制到新的文件夹Chrome里包含安装文件和Chrome数据文件     Chrome数据文件一般在"C:\Use ...

  9. notepad++列模式

    考虑下面的情况: 已有 AAA BBB CCC 和 aaa bbb ccc 想合并为 AAA aaa BBB bbb CCC ccc 在大写后面,列模式,复制小写,粘贴,结果不是如下: AAA aaa ...

  10. 一种Android数据请求框架

    大部分Android应用一般都涉及到跟server的交互,除非是某些单机应用.既然要跟server打交道,向server请求数据差点儿是必做的事情,或许每家的APP都有一套自己的详细实现逻辑.但我们还 ...