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 ...
随机推荐
- JSON数据转换成table表格
<%@ page contentType="text/html; charset=UTF-8" %> <%@taglib uri="/struts-ta ...
- jquery微博实例
1.代码实例 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...
- gpu和cpu区别
GPU的功耗远远超过CPUCache, local memory: CPU > GPU Threads(线程数): GPU > CPURegisters: GPU > CPU 多寄存 ...
- 斯坦福大学卷积神经网络教程UFLDL Tutorial - Convolutional Neural Network
Convolutional Neural Network Overview A Convolutional Neural Network (CNN) is comprised of one or mo ...
- go语言基础之获取命令行参数
1.获取命令行参数 示例: package main //必须 import "fmt" import "os" func main() { list := o ...
- HDU2089 ------不要62(数位dp)
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- UVA 270 Lining Up (几何 判断共线点)
Lining Up ``How am I ever going to solve this problem?" said the pilot. Indeed, the pilot was ...
- Asp.net视图状态的作用
视图状态(view state)是在单个页面中保存信息的第一选择,ASP.NET Web控件也使用试图状态在回发间保存属性值.通过页面内建的 ViewState 属性,你可以把自己的数据放入到视图状态 ...
- Tetris
he Tetris game is one of the most popular computer games ever created. The original game was designe ...
- 父元素没有设置定位 position absolute 解析
1.示例代码 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UT ...