LintCode 题目:链表节点计数

mac2026-04-19  11

URL:https://www.lintcode.com/problem/count-linked-list-nodes/description

描述

计算链表中有多少个节点.

样例

样例 1: 输入: 1->3->5->null 输出: 3 样例解释: 返回链表中结点个数,也就是链表的长度. 样例 2: 输入: null 输出: 0 样例解释: 空链表长度为0

思路:

链表的使用,请参考:链表   链表的基本操作

在代码段中添加:

int count; if(head==NULL) count=0; ListNode *p = head; while(p!=NULL){ count++; p = p->next; } return count;

 

 

最新回复(0)