计算一个链表中值为非负奇数的节点的个数。
样例
样例 1:
输入:1->3->5->null
输出:3
样例 2:
输入:0->null
输出:0
/**
* Definition of singly-linked-list:
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param head:
* @return: nothing
*/
int countNodesII(ListNode * head) {
//
ListNode *p;
p=head;
int cnt=0;
while(p!=NULL)
{
if(p->val>=0&&p->val%2) cnt++;
p=p->next;
}
return cnt;
}
};