版权所有, 禁止转载, 如有需要, 请站内联系.
本文地址: http://blog.csdn.net/caroline_wendy
题目:
给出一个链表和一个数k, 比如链表1→2→3→4→5→6;
若k=2, 则翻转后2→1→4→3→6→5;
若k=3, 则翻转后3→2→1→6→5→4;
若k=4, 则翻转后4→3→2→1→5→6;
用程序实现.
题目出自: 2014美团网校园招聘笔试试题 哈尔滨站 2013年9月10日
解答:
链表翻转问题, 可以使用递归进行求解, 即倒序打印, 依次倒序输出每段链表, 最后输出剩余链表;
笔试代码:
/*反转链表*/
ListNode* ReverseList (ListNode* pHead, ListNode* pTail)
{
ListNode* pReversedHead = NULL;
ListNode* pNode = pHead;
ListNode* pPrev = NULL;
while (pNode != pTail)
{
ListNode* pNext = pNode->m_pNext;
if(pNext == pTail)
pReversedHead = pNode;
pNode->m_pNext = pPrev;
pPrev = pNode;
pNode = pNext;
}
return pReversedHead;
}
/*K链表翻转*/
ListNode* KReverseList (const int k, ListNode* pHead)
{
ListNode* pReversedList = NULL;
ListNode* pReversedTail = NULL;
int index = 0;
ListNode* pStart = pHead; //起始结点
ListNode* pEnd = pHead; //结尾结点
for(int i=0; i<k; ++i) {
pEnd = pEnd->m_pNext;
if(pEnd == NULL)
break;
}
pReversedList = ReverseList (pStart, pEnd);
pReversedTail = pStart;
pStart = pEnd;
while (1)
{
if(pEnd == NULL)
break;
pEnd = pEnd->m_pNext;
++index;
if (index%k == 0) {
ListNode* pTemp = ReverseList (pStart, pEnd);
pReversedTail->m_pNext = pTemp;
pReversedTail = pStart;
pStart = pEnd;
}
}
if (pStart != NULL)
pReversedTail->m_pNext = pStart;
return pReversedList;
}
/*
* Test.cpp
*
* Created on: 2014.2.27
* Author: Spike
*/
/*eclipse cdt, gcc 4.8.1*/
#include <stdio.h>
/*链表结点*/
struct ListNode
{
int m_nKey;
ListNode* m_pNext;
};
/*打印链表*/
void PrintList(ListNode* pHead)
{
ListNode* pNode = pHead;
printf ("%d\t", pNode->m_nKey);
while (pNode->m_pNext != NULL) {
pNode = pNode->m_pNext;
printf ("%d\t", pNode->m_nKey);
}
return;
}
/*反转链表*/
ListNode* ReverseList (ListNode* pHead, ListNode* pTail)
{
ListNode* pReversedHead = NULL;
ListNode* pNode = pHead;
ListNode* pPrev = NULL;
while (pNode != pTail)
{
ListNode* pNext = pNode->m_pNext;
if(pNext == pTail)
pReversedHead = pNode;
pNode->m_pNext = pPrev;
pPrev = pNode;
pNode = pNext;
}
return pReversedHead;
}
/*K链表翻转*/
ListNode* KReverseList (const int k, ListNode* pHead)
{
ListNode* pReversedList = NULL;
ListNode* pReversedTail = NULL;
int index = 0;
ListNode* pStart = pHead; //起始结点
ListNode* pEnd = pHead; //结尾结点
for(int i=0; i<k; ++i) {
pEnd = pEnd->m_pNext;
if(pEnd == NULL)
break;
}
pReversedList = ReverseList (pStart, pEnd);
pReversedTail = pStart;
pStart = pEnd;
while (1)
{
if(pEnd == NULL)
break;
pEnd = pEnd->m_pNext;
++index;
if (index%k == 0) {
ListNode* pTemp = ReverseList (pStart, pEnd);
pReversedTail->m_pNext = pTemp;
pReversedTail = pStart;
pStart = pEnd;
}
}
if (pStart != NULL)
pReversedTail->m_pNext = pStart;
return pReversedList;
}
/*链表初始化*/
ListNode* init (void)
{
ListNode* pHead = new ListNode();
ListNode* pNode1 = new ListNode();
ListNode* pNode2 = new ListNode();
ListNode* pNode3 = new ListNode();
ListNode* pNode4 = new ListNode();
ListNode* pNode5 = new ListNode();
pHead->m_nKey = 1;
pNode1->m_nKey = 2;
pNode2->m_nKey = 3;
pNode3->m_nKey = 4;
pNode4->m_nKey = 5;
pNode5->m_nKey = 6;
pHead->m_pNext = pNode1;
pNode1->m_pNext = pNode2;
pNode2->m_pNext = pNode3;
pNode3->m_pNext = pNode4;
pNode4->m_pNext = pNode5;
pNode5->m_pNext = NULL;
return pHead;
}
/*主函数*/
int main (void)
{
const int k = 3;
ListNode* pHead = init (); //初始化链表
printf("\nOriginal List: \t");
PrintList(pHead);
printf("\nNew List: \t");
ListNode* pReversedList = KReverseList (k, pHead);
PrintList(pReversedList);
return 0;
}Original List: 1 2 3 4 5 6 New List: 3 2 1 6 5 4
编程算法/面试 - K链表翻转,布布扣,bubuko.com
原文:http://blog.csdn.net/caroline_wendy/article/details/20046027