1、查找链表的倒数第k个节点
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k)
{
if(pListHead==NULL||k==0)
return NULL;
ListNode* first=pListHead;
ListNode* second=pListHead;
for(int i=0;i<k-1;++i)
{
if(first->next!=NULL)
{
first=first->next;
}
else
{
return NULL;
}
}
while(first->next!=NULL)
{
first=first->next;
second=second->next;
}
return second;
}
2、链表逆置ListNode* ReverseList(ListNode* pHead)
{
if (NULL == pHead)
return pHead;
ListNode* pPrev = NULL;
ListNode* pCur = pHead;
ListNode* rHead = pHead;
while (pCur != NULL)
{
ListNode* pNext = pCur->next;
if (pNext != NULL)
rHead = pNext;
pCur->next = pPrev;
pPrev = pCur;
pCur = pNext;
}
return rHead;
}
3、实现一个ADD函数,不使用+、-、*、/int ADD(int num1, int num2)
{
while (num2 != 0)
{
int num = num1^num2;
num2 = (num1&num2) << 1;
num1 = num;
}
return num1;
}