反转单链表
void Reverse(ListNode *&pHead)
{
if (pHead == nullptr||pHead->_pNext==nullptr)
return;
ListNode *pCur = pHead;
ListNode *pNewHead = nullptr;
ListNode *pTemp = nullptr;
while
查找单链表的倒数第k个节点,要求只能遍历一次链表
ListNode *FindKNode(ListNode *&pHead,int k)
{
if (pHead == nullptr)
return nullptr;
ListNode *pCur = pHead;
ListNode *pFast = pHead;
ListNode *pSlow = pHead;
for (int i = 0; i < k-1; i++)
{
if (pFast->_pNext != nullptr)
pFast = pFast->_pNext;
else
return nullptr;
}
while (pFast->_pNext != nullptr)
{
pFast = pFast->_pNext;
pSlow = pSlow->_pNext;
}
return
加法器的实现
递归法:
#include<iostream>
using namespace std;
int getNum(int a, int b)
{
int wei = a ^ b;
int jinwei = (a&b) <<1;
if (jinwei == 0)
return wei;
else
return getNum(wei, jinwei);
}
int main()
{
int a;
int b;
while (cin>>a&&cin>>b)
cout
#include <iostream>
using namespace std;
int add1(int x, int y)
{
int sum;
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;
}
return sum;
}