ListNode *FindKNode(ListNode *&pHead,int k)
{
if (pHead == nullptr)
returnnullptr;
ListNode *pCur = pHead;
ListNode *pFast = pHead;
ListNode *pSlow = pHead;
for (int i = 0; i < k-1; i++)
{
if (pFast->_pNext != nullptr)
pFast = pFast->_pNext;
elsereturnnullptr;
}
while (pFast->_pNext != nullptr)
{
pFast = pFast->_pNext;
pSlow = pSlow->_pNext;
}
return pSlow;
}
加法器的实现
递归法:
#include<iostream>usingnamespacestd;
int getNum(int a, int b)
{
int wei = a ^ b;
int jinwei = (a&b) <<1;
if (jinwei == 0)
return wei;
elsereturn getNum(wei, jinwei);
}
int main()
{
int a;
int b;
while (cin>>a&&cin>>b)
cout << getNum(a, b) << endl;
}
循环法
#include <iostream>
using namespace std;
int add1(int x, int y)
{
intsum;
int carry;
int bx, by;
int base;
base = 1;
//carry用来判断是否需要进位
carry = 0;
sum = 0;
while (base != 0) {
bx = x & base;
by = y & base;
base <<= 1;
sum |= ((bx) ^ (by) ^ carry);
carry = ((bx & by) || (bx & carry) || (by & carry)) ? base : 0;
}
returnsum;
}