交换单链表中的特定元素
2013-05-16 21:46
162 查看
给定一个单链表,和数字k,交换链表中头和尾的第k个节点。检查边界情况。
输入,输出如下:
Sample Input: 1->2->3->4->5->6->7->8 and K = 3
Sample Output : 1->2->6->4->5->3->7->8
Sample Input: 1->2->3->4->5->6->7->8 and K = 10
Sample Output: print error "LIST IS OF LESSER SIZE".
Given a singly link list and a number 'K', swap the Kth node from the start with the Kth node from the last. Check all the edge cases.
Sample Input: 1->2->3->4->5->6->7->8 and K = 3
Sample Output : 1->2->6->4->5->3->7->8
Sample Input: 1->2->3->4->5->6->7->8 and K = 10
Sample Output: print error "LIST IS OF LESSER SIZE".
可以在线性情况下用三个指针解决。
第一个指针指向开始的第k个元素
第二个指针指向从结尾开始的第k个元素
最后一个指针指向结尾
遍历链表,保存指针,在最后做交换,仅仅只用交换指针所指单元中的元素。
This can be done in line without first completely traversing the list to check the size. This can be done with 3 pointers.
One pointer is for the first element which is k from the start
the second pointer is for the element which is k from the end
the last pointer is to find the end.
Then you traverse the list save the pointers and do the swap at the end, you don't even have to mess with the links when swapping, just swap the values from the pointers you have saved from above.
输入,输出如下:
Sample Input: 1->2->3->4->5->6->7->8 and K = 3
Sample Output : 1->2->6->4->5->3->7->8
Sample Input: 1->2->3->4->5->6->7->8 and K = 10
Sample Output: print error "LIST IS OF LESSER SIZE".
Given a singly link list and a number 'K', swap the Kth node from the start with the Kth node from the last. Check all the edge cases.
Sample Input: 1->2->3->4->5->6->7->8 and K = 3
Sample Output : 1->2->6->4->5->3->7->8
Sample Input: 1->2->3->4->5->6->7->8 and K = 10
Sample Output: print error "LIST IS OF LESSER SIZE".
可以在线性情况下用三个指针解决。
第一个指针指向开始的第k个元素
第二个指针指向从结尾开始的第k个元素
最后一个指针指向结尾
遍历链表,保存指针,在最后做交换,仅仅只用交换指针所指单元中的元素。
This can be done in line without first completely traversing the list to check the size. This can be done with 3 pointers.
One pointer is for the first element which is k from the start
the second pointer is for the element which is k from the end
the last pointer is to find the end.
Then you traverse the list save the pointers and do the swap at the end, you don't even have to mess with the links when swapping, just swap the values from the pointers you have saved from above.
#include <iostream> using namespace std; struct node { int data; node* next; }; node* exch_strandend_k(node* head,int k) { node* ptr1,*ptr2,*ptr3=head; int count = 0; while (ptr3->next!=NULL) { count++; if (count == k) { ptr1 = ptr3; ptr2 = head;//ptr2和ptr3相隔k个元素 当ptr3指向末尾时 ptr2指向末尾以前的第k个元素 } if (count>k) { ptr2 = ptr2->next; } ptr3 = ptr3->next;//ptr3指向链表末尾 } if (count<k)//链表元素太少 报错并返回 { cout<<"LIST IS OF LESSER SIZE"<<endl; return NULL; } if (ptr1!=NULL&&ptr2!=NULL) { int temp = ptr1->data; ptr1->data = ptr2->data; ptr2->data = temp; return head; } return NULL; }
相关文章推荐
- 面试100题之21与特定元素交换的排序算法
- LeeCode 删除单链表中的某一特定值元素
- 编写查找一个单链表特定元素的程序。分别使用递归和非递归方法实现,并比较它们的运行时间。
- 编写查找一个单链表特定元素的程序
- 带头单链表删除特定元素实现
- 如何实现单链表任意两个元素交换(不包括表头)
- 如何实现单链表交换任意两个元素(不包括头结点)
- 7,单链表交换任意两个元素(不包括表头)
- 【算法题】交换单链表任意两个元素
- 交换单链表任意两个元素(完整程序)
- java编程:两个无序数组a和b,交换a,b中的元素,使得[序列a元素的和]与[序列b元素的和]之间的差最小。
- PTA 6-2 单链表元素定位(12 分)
- 有两个数组a,b,大小都为n,数组元素的值任意,无序; 要求:通过交换a,b中的元素,使数组a元素的和与数组b元素的和之间的差最小。
- 【程序练习】——交换两数组元素,使之和差最小
- 交换单链表的任意两个节点
- Objective-C - 改变NSMutableArray的特定元素
- C/C++学习(七)单链表的头插法,尾插法,元素插入、删除和查找 .
- leetcode 99. Recover Binary Search Tree BST二叉搜索树的两元素交换的恢复 + 中序遍历 + 很棒的分析和做法
- 输入一个一维数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
- PHP删除数组中特定元素的两种方法