【LeetCode】24. 两两交换链表中的节点

解题思路

递归求解

对于自己写的递归,举例解释如下,以1-2-3-4-null为例

2->1->3->4->null
2->1->4->3->null

参照别人写的递归

2 1->3->4->null 
2 1->4  3->null
2 1->4->3->null
2->1->4->3->null

二者差异在于,我的是每次都会把head和next连接起来,而改进的是,在交换后再进行连接,因此需要保存的量少,变得也就更简洁了

代码

自己写的递归
class Solution {
public:
	ListNode* swapPairs(ListNode* head) {
		//空节点或者只剩一个节点
		if (head == nullptr || head->next == nullptr) return head;
		else
		{
			//交换列表中的前两个节点head和head.next
			ListNode* tempFirst = head;
			ListNode* tempHead = head->next;//交换后新的头节点为head.next
			ListNode* tempSecond = head->next->next;
			head->next->next = tempFirst;
			head->next = tempSecond;
			head = tempHead;//更新一下交换后的头节点
			//将交换后子列表的返回头和head.next.next相连
			head->next->next = swapPairs(head->next->next);
		}
		return head;
	}
};
参考别人写的递归
//参考别人后更简化的写法
class Solution {
public:
	ListNode* swapPairs(ListNode* head) {
		
		//空节点或者只剩一个节点
		if (head == nullptr || head->next == nullptr) return head;
		ListNode* next = head->next;
        //将子列表的返回节点与head.next相连,形成新的链表
		head->next = swapPairs(next->next);
		//将交换后的next指向head,将next和head连接起来
		next->next = head;
		return next;
	}
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值