链表翻转。给出一个链表和一个数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-04-02 21:21
369 查看
#include<iostream>
using namespace std;
typedef struct LNode {
int m_nValue;
struct LNode* m_pNext;
}LNode;
LNode* reverseList(LNode* pHead) {
if (NULL == pHead || NULL == pHead->m_pNext)
return pHead;
LNode* pNode;
LNode* pNewHead = NULL;
while (pHead) {
pNode = pHead;
pHead = pHead->m_pNext;
pNode->m_pNext = pNewHead;
pNewHead = pNode;
}
return pNewHead;
}
LNode* getLastNode(LNode* pHead) {
while (NULL != pHead->m_pNext)
pHead = pHead->m_pNext;
return pHead;
}
LNode* swapListByK(LNode* pHead, int k) {
if (k <= 1)
return pHead;
int pos;
LNode* pNode = pHead;
LNode* pNewHead;
LNode* pNextNode;
LNode* pLastNode = NULL;;
pHead = NULL;
while (pNode) {
pos = 0;
pNewHead = pNode;
while (pNode && pos < k - 1) {
pNode = pNode->m_pNext;
pos++;
}
if (pNode) {
pNextNode = pNode->m_pNext;
pNode->m_pNext = NULL;
if (NULL != pLastNode) {
pLastNode->m_pNext = NULL;
}
pNewHead = reverseList(pNewHead);
if (NULL == pHead) {
pHead = pNewHead;
} else {
pLastNode->m_pNext = pNewHead;
}
pNode = getLastNode(pNewHead);
pNode->m_pNext = pNextNode;
pLastNode = pNode;
pNode = pNextNode;
} else {
break;
}
}
return pHead;
}
void printList(LNode* pHead) {
LNode* pNode = pHead;
while (pNode) {
cout << pNode->m_nValue << " ";
pNode = pNode->m_pNext;
}
cout << endl;
}
LNode* createList(int* arr, int length) {
LNode* pHead = NULL;
LNode* pTemp, *pNode;
for (int i = 0; i < length; i++) {
pNode = (LNode*)malloc(sizeof(LNode));
pNode->m_nValue = arr[i];
pNode->m_pNext = NULL;
if (NULL == pHead)
pHead = pNode;
else
pTemp->m_pNext = pNode;
pTemp = pNode;
}
return pHead;
}
void destroyList(LNode* pHead) {
LNode* pNode;
while (pHead) {
pNode = pHead;
pHead = pHead->m_pNext;
free(pNode);
}
}
int main(int argc, char* argv[]) {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int length = sizeof(arr) / sizeof(arr[0]);
LNode* pHead = createList(arr, length);
pHead = swapListByK(pHead, 2);
printList(pHead);
destroyList(pHead);
cin.get();
return 0;
}
http://blog.csdn.net/zzran/article/details/8025886
using namespace std;
typedef struct LNode {
int m_nValue;
struct LNode* m_pNext;
}LNode;
LNode* reverseList(LNode* pHead) {
if (NULL == pHead || NULL == pHead->m_pNext)
return pHead;
LNode* pNode;
LNode* pNewHead = NULL;
while (pHead) {
pNode = pHead;
pHead = pHead->m_pNext;
pNode->m_pNext = pNewHead;
pNewHead = pNode;
}
return pNewHead;
}
LNode* getLastNode(LNode* pHead) {
while (NULL != pHead->m_pNext)
pHead = pHead->m_pNext;
return pHead;
}
LNode* swapListByK(LNode* pHead, int k) {
if (k <= 1)
return pHead;
int pos;
LNode* pNode = pHead;
LNode* pNewHead;
LNode* pNextNode;
LNode* pLastNode = NULL;;
pHead = NULL;
while (pNode) {
pos = 0;
pNewHead = pNode;
while (pNode && pos < k - 1) {
pNode = pNode->m_pNext;
pos++;
}
if (pNode) {
pNextNode = pNode->m_pNext;
pNode->m_pNext = NULL;
if (NULL != pLastNode) {
pLastNode->m_pNext = NULL;
}
pNewHead = reverseList(pNewHead);
if (NULL == pHead) {
pHead = pNewHead;
} else {
pLastNode->m_pNext = pNewHead;
}
pNode = getLastNode(pNewHead);
pNode->m_pNext = pNextNode;
pLastNode = pNode;
pNode = pNextNode;
} else {
break;
}
}
return pHead;
}
void printList(LNode* pHead) {
LNode* pNode = pHead;
while (pNode) {
cout << pNode->m_nValue << " ";
pNode = pNode->m_pNext;
}
cout << endl;
}
LNode* createList(int* arr, int length) {
LNode* pHead = NULL;
LNode* pTemp, *pNode;
for (int i = 0; i < length; i++) {
pNode = (LNode*)malloc(sizeof(LNode));
pNode->m_nValue = arr[i];
pNode->m_pNext = NULL;
if (NULL == pHead)
pHead = pNode;
else
pTemp->m_pNext = pNode;
pTemp = pNode;
}
return pHead;
}
void destroyList(LNode* pHead) {
LNode* pNode;
while (pHead) {
pNode = pHead;
pHead = pHead->m_pNext;
free(pNode);
}
}
int main(int argc, char* argv[]) {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int length = sizeof(arr) / sizeof(arr[0]);
LNode* pHead = createList(arr, length);
pHead = swapListByK(pHead, 2);
printList(pHead);
destroyList(pHead);
cin.get();
return 0;
}
http://blog.csdn.net/zzran/article/details/8025886
相关文章推荐
- 9月10日,美团网2014校招研发笔试哈尔滨站 1、链表翻转。给出一个链表和一个数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,用程序实现
- 链表翻转。给出一个链表和一个数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,用程序实现
- 链表翻转。给出一个链表和一个数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
- 链表翻转。给出一个链表和一个数k,比如链表1→2→3→4→5→6,k=2,翻转后2→1→4→3→6→5。
- 链表翻转。给出一个链表和一个数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
- 面试题:题目: 链表翻转。给出一个链表和一个数k,比如链表1→2→3→4→5→6,k=2,翻转后2→1→4→3→6→5
- 链表翻转。比如给出一个链表和一个数k,比如链表1→2→3→4→5→6,k=2,翻转后2→1→4→3→6→5
- 链表翻转(给出一个链表和一个数k,比如链表1→2→3→4→5→6,k=2,翻转后2→1→4→3→6→5)
- 对于一颗完全二叉树,要求给所有节点加上一个pNext指针,指向同一层的相邻节点;如果当前节点已经是该层的最后一个节点,则将pNext指针指向NULL;给出程序实现,并分析时间复杂度和空间复杂度。
- 怎样编写一个程序,把一个有序整数数组放到二叉树中? 编写实现链表排序的一种算法。说明为什么你会选择用这样的方法?
- 链表翻转:给出一个链表和一个数k,链表前k个节点进行翻转
- 编写一个程序,实现删除链表中倒数第k个节点(用单链表实现)
- 关于用栈链表来实现中序表达式的一个小程序 《数据结构》
- 利用模板类编写一个程序,实现双向链表的插入、删除、查找、显示的功能。
- 对于一颗完全二叉树,要求给所有节点加上一个pNext指针,指向同一层的相邻节点;如果当前节点已经是该层的最后一个节点,则将pNext指针指向NULL;给出程序实现,并分析时间复杂度和空间复杂度。
- 实现一个无锁的Stack,并写一段测试代码(多线程访问),证明这个Stack是线程安全的。给出程序以及运行的截图。
- 一个 while 实现 C 翻转单向链表
- n个并发进程共用一个公共变量Q,写出用信号灯实现n个进程互斥的程序描述,给出信号灯值得取值范围,并说明每个取值范围的物理意义。
- 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。 比如字符串“abacacde”过滤结果为“abcde”。 要求实现函
- (学习java)写一个完整的程序,实现随机生成20个元素的链表,快速查找中间结点的值并显示