将链表中的所有元素为奇数的节点移到元素为偶数节点的前面,并使奇数之间顺序反转,偶数之间顺序反转。

将链表中的所有元素为奇数的节点移到元素为偶数节点的前面,并使奇数之间顺序反转,偶数之间顺序反转。

示例:

交换前链表的顺序           交换后链表的顺序

4→5→7→1→6   ==>  1→7→5→6→4

1                ==>  1                   (链表仅含一个元素)

2→1            ==>  1→2 

                   ==>                        (链表为空)

 

C/C++:

链表节点定义为:

struct node {

struct node *next;

int value;

};

struct node*swap(struct node *list);

#include<iostream>

using namespace std;

struct node
{
	struct node *next;
	int value;
};


node * CreateListNode(int value)//链表中创建节点
{
	if(value == NULL)
		return NULL;
	node *pNode = new node();
	pNode->value = value;
	pNode->next =NULL;
	return pNode;
}

void ConnectListNodes(node *pCurrent, node *pNext)	// 连接两个节点
{
	if(pCurrent == NULL)
	{
		cout<<"Error to connect two nodes."<<endl;
		exit(1);
	}
	pCurrent->next = pNext;
}

void PrintList(node *pHead)
{
	cout<<"PrintList start."<<endl;
	node *pNode = pHead;
	while(pNode != NULL)
	{
		cout<<pNode->value;
		pNode = pNode->next;
	}
	cout<<endl;
	cout<<"PrintList"<<endl;
}

void Destroy(node *pHead)
{
	node *pNode = pHead;
	while(pNode != NULL)
	{
		pHead = pHead->next;
		delete pNode;
		pNode = pHead;
	}
}

//返回,p所对应位置的下一个位置对应的值
 int dqExchage(struct node *list1,struct node *list2,struct node *list3,int p)
 {
 	if(p %2 ==0){//偶数
		while(list1->next && list1->value % 2 !=0){
 			list1=list1->next;
 		}
 		list2=list1;		
 	}
 	
	int tmp=list1->value;
	int q = p;
 	while( list1->next && list1->value !=q){
		list1->value = p;
		p = tmp;
		list1 = list1->next;
		tmp = list1->value;
		
 	}
	list1->value = p;
	
	if(list1->next){
		return list1->next->value;
 	}else{
 		return NULL;
 	}
 	
 }
 //思路分析
 // 特殊的插入排序
 //针对奇数,插入到最前面,针对偶数,插入到前面奇数的后面,使其成为第一个偶数
struct node *swap(struct node *list)
{	
	node *pHead = list;
	node *pHead1 = list;

	//0
	if(list->next){
		int p=list->next->value;
		while(p != NULL){
			p=dqExchage(list, list, list,p);
		
		}
		return list;
	}else{
		return NULL;
	}

}



void Test1()
{
	node *pNode1 = CreateListNode(4);
	node *pNode2 = CreateListNode(5);
	node *pNode3 = CreateListNode(7);
	node *pNode4 = CreateListNode(1);
	node *pNode5 = CreateListNode(6);

	ConnectListNodes(pNode1, pNode2);
	ConnectListNodes(pNode2, pNode3);
	ConnectListNodes(pNode3, pNode4);
	ConnectListNodes(pNode4, pNode5);
	PrintList(pNode1);
	PrintList(swap(pNode1));
}

void Test2()
{
	node *pNode1 = CreateListNode(4);
	node *pNode2 = CreateListNode(2);
	node *pNode3 = CreateListNode(7);
	node *pNode4 = CreateListNode(8);
	node *pNode5 = CreateListNode(6);

	ConnectListNodes(pNode1, pNode2);
	ConnectListNodes(pNode2, pNode3);
	ConnectListNodes(pNode3, pNode4);
	ConnectListNodes(pNode4, pNode5);
	PrintList(pNode1);
	
	PrintList(swap(pNode1));
}


int main()
{
	cout<<"Test1"<<endl;

	Test1();
	cout<<"Test2"<<endl;
	Test2();
	//
	system("pause");
	return 0;
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以按照以下步骤将单链表l1拆成两个链表: 1. 创建两个新的链表节点l1_new和l2_new,分别表示原链表奇数序号节点偶数序号节点。 2. 遍历原链表l1,将奇数序号节点插入到l1_new链表的末尾,将偶数序号节点插入到l2_new链表的末尾。 3. 最后,将l2_new链表反转,使其链接方向与l1相反。 具体实现可以参考以下代码: // 定义链表节点结构体 struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; // 将单链表l1拆成两个链表 void splitList(ListNode* l1, ListNode* &l1_new, ListNode* &l2_new) { ListNode *p1 = l1, *p2 = NULL, *p3 = NULL; l1_new = new ListNode(); l2_new = new ListNode(); p2 = l1_new; p3 = l2_new; int count = 1; while (p1 != NULL) { if (count % 2 == 1) { p2->next = new ListNode(p1->val); p2 = p2->next; } else { p3->next = new ListNode(p1->val); p3 = p3->next; } p1 = p1->next; count++; } // 反转l2_new链表 ListNode *prev = NULL, *curr = l2_new->next, *next = NULL; while (curr != NULL) { next = curr->next; curr->next = prev; prev = curr; curr = next; } l2_new->next = prev; } // 测试函数 int main() { ListNode *l1 = new ListNode(1); l1->next = new ListNode(2); l1->next->next = new ListNode(3); l1->next->next->next = new ListNode(4); l1->next->next->next->next = new ListNode(5); ListNode *l1_new = NULL, *l2_new = NULL; splitList(l1, l1_new, l2_new); // 输出l1_new链表 ListNode *p = l1_new->next; while (p != NULL) { cout << p->val << " "; p = p->next; } cout << endl; // 输出l2_new链表 p = l2_new->next; while (p != NULL) { cout << p->val << " "; p = p->next; } cout << endl; return ; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值