24.两两交换链表中的节点 25.K个一组翻转链表(hard)

24.两两交换链表中的节点

1.分成偶数节点与奇数节点两个链表,拼接(自己的思路)

时间空间复杂度都为O(n)需要额外空间储存链表

class Solution(object):
    def sampleList(self,head):
    	###p,p2分别指向奇数节点与偶数节点##
        dummy=ListNode(next=head)
        p=dummy
        dummy2=ListNode(next=head.next)
        p2=dummy2
        ##使用try语句来处理边界##
        try:
            while 1:
                p.next=head
                p=p.next
                p2.next=head.next
                p2=p2.next
                head=head.next.next
        except:
        ###得到奇数结点与偶数结点组成的两个链表###
            return dummy.next,dummy2.next
    def swapPairs(self, head):
        if head==None or head.next==None:return head
        l1,l2=self.sampleList(head)
        dummy=ListNode()
        p=dummy
        ##先偶数再奇数的顺序拼接##
        while l1 and l2:
            p.next=l2
            p=p.next
            l2=l2.next
            p.next=l1
            p=p.next
            l1=l1.next
        return dummy.next

2.递归

停止条件:head中只有一个或0个Node
f=改变head、head.next的顺序+f(剩下的链表)
return 原来head.next节点

class Solution:
    def swapPairs(self, head: ListNode) -> ListNode:
        if head==None or head.next==None:return head
        r=head.next#用来储存反转后的头节点
        p=self.swapPairs(head.next.next)
        head.next.next=head
        head.next=p
        return r

在这里插入图片描述

3.迭代

使用cur指针记录当前位置,初始指向dummy
每次反转cur之后的两个节点node1,node2
cur→node1→node2→变为cur→node2→node1
然后重置cur指针指向Node1
边界:cur 之后只有1个节点或0个节点

class Solution:
    def swapPairs(self, head: ListNode) -> ListNode:
        dummy=ListNode(next=head)
        cur=dummy
        while cur.next and cur.next.next:
            node1,node2=cur.next,cur.next.next
            node1.next=node2.next
            node2.next=node1
            cur.next=node2
            cur=node1
        return dummy.next

25.K个一组翻转链表

在这里插入图片描述

1迭代:

设置一个step变量记录步数,cur指针记录每一段的第一个节点,每k步断开链表,翻转cur
断开时,pre指针指向这一段链表前一个节点,nex指针指向原链表的下一个节点
翻转完成后,pre.next指向反转后的链表,重置pre,指向翻转链表的尾结点tail,同时重置head从nex开始下一轮计数(需要判断nex是否为None)

class Solution(object):
	##翻转链表,返回翻转后链表的头结点与尾节点##
    def reverseList(self,head):
        tail,cur=head,head
        pre=None
        while cur:
            next=cur.next
            cur.next=pre
            pre=cur
            cur=next
        return pre,tail
    def reverseKGroup(self, head, k):
        dummy=ListNode()
        pre=dummy
        #cur用来记录当前片段的头结点#
        cur=head
        step=1
        while head:
            if step==k:
                nex=head.next#记录断开处的下一个节点
                head.next=None#断开
                #翻转当前链表片段,然后与前一段链表相连接#
                pre.next,tail=self.reverseList(cur)
                #重置pre指针,指向末节点#
                pre=tail
                #判断断开处是否是原链表的末尾#
                if nex:
                	#重置head、cur、步数#
                    head=nex
                    cur=head
                    step=1
                else:
                    cur=None
                    break
            head=head.next
            step+=1
        #连接链表最后的片段#
        pre.next=cur
        return dummy.next

在这里插入图片描述

2.递归

停止条件:剩下节点数小于k
f=反转前k个节点+f(剩下的节点)

class Solution:
    def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
        cur = head
        count = 0
        while cur and count!= k:
            cur = cur.next
            count += 1
        if count == k:
            cur = self.reverseKGroup(cur, k)
            while count:
                tmp = head.next
                head.next = cur
                cur = head
                head = tmp
                count -= 1
            head = cur   
        return head
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值