链表翻转。给出一个链表和一个数k,比如链表1→2→3→4→5→6,k=2,翻转后2→1→4→3→6→5。
2017-07-23 11:13
555 查看
链表翻转。给出一个链表和一个数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,用程序实现Node* RotateList(Node* list,
size_t k).
思路:取出长度为k的链表将其翻转,在链接到链表里。
#include<iostream>
using namespace std;
typedef struct LNode {
int Value;
struct LNode* Next;
}LNode;
LNode* reverseList(LNode* pHead) {
if (NULL == pHead || NULL == pHead->Next)
return pHead;
LNode* pNode;
LNode* pNewHead = NULL;
while (pHead) {
pNode = pHead;
pHead = pHead->Next;
pNode->Next = pNewHead;
pNewHead = pNode;
}
return pNewHead;
}
LNode* getLastNode(LNode* pHead) {
while (NULL != pHead->Next)
pHead = pHead->Next;
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->Next;
pos++;
}
if (pNode)
{
pNextNode = pNode->Next;
pNode->Next = NULL;
if (NULL != pLastNode)
{
pLastNode->Next = NULL;
}
pNewHead = reverseList(pNewHead);
if (NULL == pHead)
pHead = pNewHead;
else
pLastNode->Next = pNewHead;
pNode = getLastNode(pNewHead);
pNode->Next = pNextNode;
pLastNode = pNode;
pNode = pNextNode;
}
else
break;
}
return pHead;
}
void printList(LNode* pHead) {
LNode* pNode = pHead;
while (pNode) {
cout << pNode->Value << " ";
pNode = pNode->Next;
}
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->Value = arr[i];
pNode->Next = NULL;
if (NULL == pHead)
pHead = pNode;
else
pTemp->Next = pNode;
pTemp = pNode;
}
return pHead;
}
void destroyList(LNode* pHead) {
LNode* pNode;
while (pHead) {
pNode = pHead;
pHead = pHead->Next;
free(pNode);
}
}
int main()
{
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, 4);
printList(pHead);
destroyList(pHead);
system("pause");
return 0;
}
size_t k).
思路:取出长度为k的链表将其翻转,在链接到链表里。
#include<iostream>
using namespace std;
typedef struct LNode {
int Value;
struct LNode* Next;
}LNode;
LNode* reverseList(LNode* pHead) {
if (NULL == pHead || NULL == pHead->Next)
return pHead;
LNode* pNode;
LNode* pNewHead = NULL;
while (pHead) {
pNode = pHead;
pHead = pHead->Next;
pNode->Next = pNewHead;
pNewHead = pNode;
}
return pNewHead;
}
LNode* getLastNode(LNode* pHead) {
while (NULL != pHead->Next)
pHead = pHead->Next;
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->Next;
pos++;
}
if (pNode)
{
pNextNode = pNode->Next;
pNode->Next = NULL;
if (NULL != pLastNode)
{
pLastNode->Next = NULL;
}
pNewHead = reverseList(pNewHead);
if (NULL == pHead)
pHead = pNewHead;
else
pLastNode->Next = pNewHead;
pNode = getLastNode(pNewHead);
pNode->Next = pNextNode;
pLastNode = pNode;
pNode = pNextNode;
}
else
break;
}
return pHead;
}
void printList(LNode* pHead) {
LNode* pNode = pHead;
while (pNode) {
cout << pNode->Value << " ";
pNode = pNode->Next;
}
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->Value = arr[i];
pNode->Next = NULL;
if (NULL == pHead)
pHead = pNode;
else
pTemp->Next = pNode;
pTemp = pNode;
}
return pHead;
}
void destroyList(LNode* pHead) {
LNode* pNode;
while (pHead) {
pNode = pHead;
pHead = pHead->Next;
free(pNode);
}
}
int main()
{
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, 4);
printList(pHead);
destroyList(pHead);
system("pause");
return 0;
}
相关文章推荐
- 链表翻转。给出一个链表和一个数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,用程序实现
- 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,比如链表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,按照分组逆置。
- 链表翻转:给出一个链表和一个数k,链表前k个节点进行翻转
- flag4给出俩个单向链表的头指针,比如pA,pB,判断这俩个链表是否相交
- CPP一个链表,数据的增删改查(不包括翻转、排序,合并等)等,Node* &的使用
- <仅是自己做笔记。。。系列-6>判断俩个链表是否相交 给出俩个单向链表的头指针,比如h1,h2,判断这俩个链表是否相交。
- 递归地翻转一个链表
- 给定一个链表,比如L1--->L2---->L3---->................----->Ln,把链表调整为L1---->Ln----->L2----->Ln-1------>L3----
- 一个 while 实现 C 翻转单向链表
- 翻转链表 给定一个链表个一个整数k 将从右边k个翻转到前边
- 链表翻转【比如链表1→2→3→4→5→6,k=2, 翻转后2→1→4→3→6→】
- 请给出一个时间为O(nlgk)、用来将k个已排序链表的算法。此处n为所有输入链表中元素的总数。
- 给定一个链表的头指针,要求只遍历一次,将单链表中的元素的顺序翻转过来