单链表交换任意两个元素

思路:

有两种情况,相邻和不相邻。

首先找两个节点的前驱,可以通过前驱来判断是否相邻。

相邻则改变3个结点的next指针,不相邻则改变4个结点的next指针。

 

 

// LinkTable.cpp : 定义控制台应用程序的入口点。
//
 
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
 
//链表的结构体
struct node
{
	char val;
	node * next;
};
 
//建立链表
struct node * create( string & str_link )
{
	int len = str_link.length();
 
	struct node * phead = new node();     //带有表头的链表,表头中不存储任何元素
	struct node * preNode = phead;
	for( int i=0; i<len; i++ )
	{
		struct node * pNode = new node();
		pNode->val = str_link[i];
		pNode->next = NULL;
		preNode->next = pNode;
		preNode = pNode;
	}
	return phead;
}
 
//输出链表
void out_link( struct node * phead )
{
	if( phead == NULL )
		return;
	struct node * pNode = phead->next;
	while( pNode )
	{
		cout <<pNode->val;
		pNode = pNode->next;
	}
	cout << endl;
}
 
//找到第index个元素
struct node * find_node(struct node* phead, int index )
{
	if(!phead) return NULL;
	struct node * pNode = phead;
	while( index--)
	{
		pNode = pNode->next;
		if( !pNode )
			return NULL;
	}
	return pNode;
}
 
//找前驱节点
struct node* find_preNode( struct node* phead, struct node* pNode)
{
	if( !pNode ) return NULL;
	struct node* preNode = phead;
	while( preNode )
	{
		if( preNode->next == pNode )
			return preNode;
		preNode = preNode->next;
	}
	return NULL;
}
 
//交换节点
void exchange_node( struct node* phead, struct node* pNode1, struct node* pNode2)
{
	if( !phead ) return;
	//分别找到pNode1的前驱pPre1,和pNode2的前驱pPre2
	struct node* pPre1 = find_preNode( phead, pNode1);
	struct node* pPre2 = find_preNode( phead, pNode2);
	if( !pPre1 || !pPre2 ) return;  //有任何一个没有找到
 
	//相邻的情况
	struct node *pBefore, *pAfter, *pPre;
	if( pPre2 == pNode1 )
	{
		pBefore = pNode1;
		pAfter = pNode2;
		pPre = pPre1;
	}
	if( pPre1 == pNode2)
	{
		pBefore = pNode2;
		pAfter = pNode1;
		pPre = pPre2;
	}
	if( pPre2 == pNode1 || pPre1 == pNode2 )
	{
		pPre->next = pAfter;
		pBefore->next = pAfter->next;
		pAfter->next = pBefore;
	}
	else
	{
		//不相邻的情况
		struct node *pNext1 = pNode1->next;
		pNode1->next = pNode2->next;
		pPre1->next = pNode2;
		pPre2->next = pNode1;
		pNode2->next = pNext1;
	}
 
}
 
void test()
{
	string str;
	cin >> str;
	struct node *phead = create( str );
	
	int index;
	cin >> index;
	struct node * pNode1 = find_node( phead, index );
	cin >> index;
	struct node * pNode2 = find_node( phead, index );
	exchange_node( phead, pNode1, pNode2 );
	cout << "after exchange: "<<endl;
	out_link( phead );
 
}
 
int _tmain(int argc, _TCHAR* argv[])
{
	test();
	return 0;
}

 

在Python中,交换链表中前后第K个元素是一个涉及到链表操作的问题。在进行这个操作之前,需要明确链表的结构,以及K值的有效性。假设我们有一个单向链表,我们将按照以下步骤进行操作: 1. 首先,我们需要遍历链表找到第K个节点。 2. 然后,我们需要找到第K个节点之后的第K个节点(即倒数第K个节点)。 3. 接着,我们需要交换个节点的位置。 这里提供一个简单的Python代码示例来说明如何交换链表中前后第K个元素: ```python class ListNode: def __init__(self, value=0, next=None): self.value = value self.next = next def swapNodes(head, k): # Step 1: Find the kth node from the beginning kth_node = head for i in range(k - 1): if not kth_node: return None # K is larger than the length of the list kth_node = kth_node.next # Step 2: Find the kth node from the end kth_from_end = head for i in range(k): if not kth_from_end: return None # K is larger than the length of the list kth_from_end = kth_from_end.next # If kth_from_end is None, k is invalid or list is shorter than k if kth_from_end is None: return None # Step 3: Swap the values of kth_node and kth_from_end kth_node.value, kth_from_end.value = kth_from_end.value, kth_node.value return head # Helper function to print the list def printList(head): current = head while current: print(current.value, end=" ") current = current.next print() # Example usage: # Create the linked list 1->2->3->4->5 head = ListNode(1) head.next = ListNode(2) head.next.next = ListNode(3) head.next.next.next = ListNode(4) head.next.next.next.next = ListNode(5) print("Original list:") printList(head) # Swap the 2nd and 4th nodes head = swapNodes(head, 2) print("List after swapping:") printList(head) ``` 在上述代码中,我们定义了一个`ListNode`类来表示链表节点,并提供了`swapNodes`函数来交换链表中前后第K个元素。我们还定义了一个`printList`函数来打印链表的内容,以便于验证我们的交换操作是否成功。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值