c_cpp 交换第k个节点

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 交换第k个节点相关的知识,希望对你有一定的参考价值。

/*
http://ideone.com/IecjJp
http://www.geeksforgeeks.org/swap-kth-node-from-beginning-with-kth-node-from-end-in-a-linked-list/
http://www.practice.geeksforgeeks.org/problem-page.php?pid=535
*/

#include <iostream>
#include <string>
#include <vector>
using namespace std; 

int main() {
	int t, n, k;
	cin >> t;
	while(t--){
		cin >> n >> k;
		if(n < k){
			cout << k << " is bigger than " << n <<endl;
			return 0;
		}
		int a[1001];
		for(int i=0; i<n; i++)
			cin >> a[i];
			
		int temp = a[k-1];
		a[k-1] = a[n-k];
		a[n-k] = temp;
		
		for(int i=0; i<n; i++){
			cout << a[i];
			cout << ((i==n-1) ? "\n" : " ");
		}
	}
	return 0;
}

以上是关于c_cpp 交换第k个节点的主要内容,如果未能解决你的问题,请参考以下文章

力扣1721.交换链表中的结点(中)

交换链表中的节点

leetcode 1721. 交换链表中的节点

c_cpp 找到BST的第k个最小节点

LeetCode Algorithm 1721. 交换链表中的节点

LeetCode Algorithm 1721. 交换链表中的节点