【每日一题-2】链表基础面试题

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;inext!=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;
}

你可能感兴趣的:(刷题,每日一题)