【算法题】交换单链表任意两个元素
2017-05-29 10:08
246 查看
首先添加虚拟头结点
然后找到指定节点的前驱节点
再然后分两种情况进行交换:相邻节点交换和非相邻节点交换
然后找到指定节点的前驱节点
再然后分两种情况进行交换:相邻节点交换和非相邻节点交换
#include <iostream> #include <vector> #include <numeric> #include <algorithm> using namespace std; struct ListNode { ListNode* next; int value; }; bool List_insert(ListNode ** phead, int x, int i) { //空指针 if (phead == NULL) { return 0; } ListNode * pCurrent(NULL); //头插入 if (i==1) { pCurrent = *phead; ListNode * pNew = new ListNode; pNew->value = x; pNew->next = pCurrent; *phead = pNew; return 1; } //非头插入 pCurrent = *phead; ListNode * pfront(NULL); int k(1); while (k < i && pCurrent != NULL) { pfront = pCurrent; pCurrent = pCurrent->next; k++; } if (k != i) { return 0; } ListNode * pNew = new ListNode; pNew->value = x; pNew->next = pCurrent; pfront->next = pNew; return 1; } void List_print(ListNode * phead) { while (phead!=NULL) { cout << phead->value; phead = phead->next; } cout << endl; } ListNode * findpre(ListNode* phead,int v)//寻找前驱节点 { if (phead==NULL) { return phead; } for (auto i = 0; i < v-1;i++) { if (phead!=NULL) { phead = phead->next; } } return phead; } ListNode* swapnode(ListNode * head,int v1,int v2)//交换第v1和v2个节点 { if (head==NULL|| head->next==NULL||v1==v2) { return head; } ListNode dummy; dummy.next = head; if (v1>v2) { std::swap(v1, v2); } ListNode * v1_pre = findpre(&dummy, v1); ListNode * v2_pre = findpre(&dummy, v2); if (v2_pre == NULL||v2_pre->next == NULL) { return head; } if (v1_pre->next == v2_pre)//相邻v1 == v2_pre { ListNode* v2_node = v2_pre->next; v1_pre->next = v2_node; v2_pre->next = v2_node->next; v2_node->next = v2_pre; } else { ListNode * v1_node = v1_pre->next; ListNode * v2_node = v2_pre->next; std::swap(v1_node->next, v2_node->next); std::swap(v1_pre->next, v2_pre->next); } return dummy.next; } int main() { ListNode * phead = NULL; List_insert(&phead, 1, 1); List_insert(&phead, 2, 1); List_insert(&phead, 3, 1); List_insert(&phead, 4, 1); List_insert(&phead, 5, 1); List_insert(&phead, 6, 1); List_print(phead); phead = swapnode(phead,5,7 ); List_print(phead); phead = swapnode(phead, 5, 1); List_print(phead); return 0; }
相关文章推荐
- 每天学习一算法系列(29)(有两个序列a,b,大小都为n,序列元素的值任意整数,无序;要求:通过交换a,b 中的元素,使[序列a 元素的和]与[序列b 元素的和]之间的差最小)
- 交换单链表任意两个元素(完整程序)
- 7,单链表交换任意两个元素(不包括表头)
- 如何实现单链表任意两个元素交换(不包括表头)
- 如何实现单链表交换任意两个元素(不包括头结点)
- 有两个序列a,b,大小都为n,序列元素的值任意整数,无序; 要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小。
- 有两个数组a,b,大小都为n,数组元素的值任意,无序;要求:通过交换a,b中的元素,使数组a元素的和与数组b元素的和之间的差最小
- [微软]有两个序列a,b,大小都为n,序列元素的值任意整数,无序; 要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小
- 交换单链表的任意两个节点
- 华为面试题 题目:有两个数组a,b,大小都为n,数组元素的值任意,无序; 要求:通过交换a,b中的元素,使数组a元素的和与数组b元素的和之间的差最小
- php 求一个无序数组经过排列后任意两个相邻元素之差的最大值(算法)
- 有两个数组a,b,大小都为n,数组元素的值任意,无序;要求:通过交换a,b中的元素,使数组a元素的和与数组b元素的和之间的差最小
- 有两个序列a,b,大小都为n,序列元素的值任意整数,无序;要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小。
- 华为面试题(8分钟写出代码) 有两个数组a,b,大小都为n,数组元素的值任意,无序; 要求:通过交换a,b中的元素,使数组a元素的和与数组b元素的和之间的差最小
- 建立单链表并交换表中任意两个元素
- 微软算法100题14 在排序数组中找到和为指定数的任意两个元素
- 《数组-规划》 有两个序列a,b,大小都为n,序列元素的值任意整数,无序;要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小
- 【学习点滴-数据结构-单链表】交换单链表中任意两个元素
- 有两个序列a,b,大小都为 n,序列元素的值任意整数,无序 通过交换a,b中的元素,使序列 a的和与序列b的和之间的差最小
- 有两个序列a,b,大小都有n,序列元素的值任意整数,无序; 要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b无素的和]之间的差最大。