【leetcode】两两交换链表中的节点 c++ python

题目描述:
给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。

示例 1:
在这里插入图片描述

输入:head = [1,2,3,4]
输出:[2,1,4,3]

示例2:

输入:head = []
输出:[]

示例3:

输入:head = [1]
输出:[1]

提示:

链表中节点的数目在范围 [0, 100] 内
0 <= Node.val <= 100

c++代码(非递归):

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    //非递归解法
    ListNode* swapPairs(ListNode* head) { 
        if(head==nullptr||head->next==nullptr)return head;
        ListNode* newhead = new ListNode(-1,head);
        ListNode* temp = newhead;
        ListNode* newnext = newhead;
        ListNode* newbehind = head;
        while(temp->next!=nullptr&&temp->next->next!=nullptr){
            newnext = temp->next;
            newbehind = temp->next->next;
            temp->next = newbehind;
            newnext->next = newbehind->next; //!让newnext的下一个指向和newbehind一样,保证后面部分的链表不变
            newbehind->next = newnext;
            temp = newnext;
        }
        return newhead->next;
    }
};

1.首先声明newhead作为虚头节点,指向head,用newhead->next记录变化后的head。再声明temp,初始化temp=newhead,用temp来遍历链表

2.用temp遍历链表,当temp不满足其后面还有两个非空节点时,结束遍历;若满足,则执行交换操作:

【要两两交换,每次变化只涉及3个节点,当前节点a,和a后面两个要交换顺序的节点b和c(a->b->c变成a->c->b),下一轮变化时,让a等于b,再从这里开始新的交换】

首先用newnext记录要后移的节点newbehind记录要前移的节点。如:1->2->3->4中,newnext为2,newbehind为1,newnext为4,newbehind为3。

然后更新节点间的next关系
(1)更新temp->next,前移节点:temp->next = newbehind;
(2)更新newnext->next,使其指向原先newbehind后面的节点,如1->2->3->4中,newnext->next应该更新为指向3:newnext->next = newbehind->next;
(3)更新newbehind->next,后移节点:newbehind->next = newnext;

然后移动temp,开启下一轮的变化:temp = newnext

3.最后通过temp已经修改了链表,使得newhead后面指向的是新的链表,只需return newhead->next

c++代码(递归):

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    //递归解法
    ListNode* swapPairs(ListNode* head) { 
        //递归终止条件:当当前节点为空,或当前节点后面只有一个节点时不再执行变换操作,直接return
        if(head==nullptr||head->next==nullptr)return head; 
        ListNode* nextnode = head->next;
        head->next = swapPairs(head->next->next);
        nextnode->next = head;
        return nextnode;
    }
};

在这里插入图片描述

python代码(非递归):

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def swapPairs(self, head):
        if head == None or head.next == None:
            return head
        newhead = ListNode(-1,head) #新虚头节点
        temp = newhead #用于遍历链表
        while temp.next != None and temp.next.next != None:
            #当前节点后面两个要交换的节点
            newnext = temp.next
            newbehind =  newnext.next
            #执行交换
            temp.next = newbehind
            newnext.next = newbehind.next
            newbehind.next = newnext
            #更新当前节点
            temp = newnext
        #返回虚节点后面的第一个节点,即为修改后的链表的头节点
        return newhead.next

在这里插入图片描述

(python里空是None,不是NULL)

(== None ,!= None 和 is None, is not None 都可以)

注意:
(1)a->x->y->b,交换两个节点x和y让a指向y,让x指向b,再让y指向x
(2)返回修改链表后的新头节点:先创建虚头节点newhead指向原链表头节点,再创建新的头节点temp(temp=head)用于遍历和修改链表中的节点,最后返回newhead->next即为修改后链表的头节点。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值