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 ...
随机推荐
- jquery hide和show方法
html 元素的显示和隐藏hide() 如果被选的元素已被显示,则隐藏该元素. show() 如果被选的元素已被隐藏,则显示该元素. <html> <head> <scr ...
- RTF文件合并 备忘
多个独立的rtf文件合并时,如果文件纸张方向存在横向 纵向不一致时,文件之间需要插入分节符 \pard \ltrpar\qj \li0\ri0\nowidctlpar\wrapdefault\aspa ...
- 设计模式学习--迭代器模式(Iterator Pattern)和组合模式(Composite Pattern)
设计模式学习--迭代器模式(Iterator Pattern) 概述 ——————————————————————————————————————————————————— 迭代器模式提供一种方法顺序 ...
- Resin install document
Centos6快速安装文档 resin3.1.13 软件下载地址: http://caucho.com/products/resin/download/gpl#download #系统环境[root@ ...
- C++数组参数应用方式探讨(转)
对于经验丰富的编程人员来说,C++编程语言应该是他们经常使用于程序开发的一种实用性语言.那么,在C++中,C++数组参数永远不会按值传递.它是传递第一个元素(准确地说是第0个)的指针. 例如,如下声明 ...
- 用php编写我的第一段代码:hello world
一.php环境的搭建 在编写php前,先搭建php环境,我选择了一站式安装软件WampServer,WampServer的下载地址:http://www.wampserver.com/ WampSer ...
- Cocos2d-x -- 图片菜单按钮
Scene* MainMenu::createScene() { // 'scene' is an autorelease object auto scene = Scene::create(); / ...
- javascript - = 、==、===、!=、!==、&&、||、!
= .==.===.!=.!==.&&.||.! /* * = .==.===.!=.!==.&&.||.! */ var a = 1; var b = 1; var ...
- 算法笔记_026:折半查找(Java)
目录 1 问题描述 2 解决方案 2.1 递归法 2.2 迭代法 1 问题描述 首先,了解一下何为折半查找?此处,借用<算法设计与分析基础>第三版上一段文字介绍: 2 解决方案 2.1 递 ...
- Drag & drop a button widget
In the following example, we will demonstrate how to drag & drop a button widget. #!/usr/bin/pyt ...