算法:字节跳动单向链表K长度一组逆序重组

链表反转:给定一个单链表,每k个为一组,一组内的元素反转。
如输入: 1-2-3-4-5-6-7-8,k=3,
输出:3-2-1-6-5-4-8-7。
==每个节点的指针地址不变,不能改值==

利用栈即可,什么特么的不仔细看题,自己给自己加戏,以为要改指针形成新链表,活该自己倒霉了

代码 python2:

# -*- coding: utf-8 -*-
"""
Created on Wed Feb  3 16:13:38 2021

@author: 95136
"""
#不改变指针啊,傻逼了
import sys
#str = raw_input()
#print str
#print 'Hello,World!'
#arr=sys.stdin.readline()
class Node:
    def __init__(self,val):
        self.val=val
        self.next=None

def transformLinkListByK(root,k):
    count=0
    ans=[]
    templist=[]
    head=None
    tail=None
    if not root:
        return root 
    cur=root
    while cur:
        templist.append(cur)
        cur=cur.next
        count+=1
        if count==k:
            while templist:
                ans.append(templist.pop().val)
            count=0
        
    while templist:
        ans.append(templist.pop().val)
    return ans

root=None
#nodelist=raw_input().split("-")
nodelist=['1','2','3','4','5','6','7','8']
#k=int(raw_input())
k=3
if nodelist:
    root=Node(nodelist[0])
else:
    raise(Exception("input invalid!"))
cur=root
for val in nodelist[1:]:
    cur.next=Node(val)
    cur=cur.next
arr=transformLinkListByK(root,k)
print "-".join(arr)

在这里插入图片描述

#下面放改指针的东西,比较复杂点,饶。
#然后牛客网有一题不一样,是最后剩下不满k的,不反转,维持原样,不一样的
python2代码

# -*- coding: utf-8 -*-
"""
Created on Wed Feb  3 16:13:38 2021

@author: 95136
"""

import sys
#str = raw_input()
#print str
#print 'Hello,World!'
#arr=sys.stdin.readline()
class Node:
    def __init__(self,val):
        self.val=val
        self.next=None

def transformLinkListByK(root,k):
    count=0
    templist=[]
    head=None
    tail=None
    if not root:
        return root 
    cur=root
    while cur:
        templist.append(cur)
        cur=cur.next
        count+=1
        if count==k:
            curhead,curtail = process(templist)
            if not head:
                head=curhead
                tail=curtail
            else:
                tail.next=curhead
                tail=curtail
            templist=[]
            count=0
    if templist:
        curhead,curtail = process(templist)
        if not head:
            head=curhead
            tail=curtail
        else:
            tail.next=curhead
            tail=curtail
    tail.next=None
    return head
def process(templist):
    curhead=None
    cur=None
    curtail=None
    while templist:
        node=templist.pop()
        if not cur:
            cur=node
            curhead=cur
        else:
            cur.next=node
            cur=cur.next
        if len(templist)<1:
            curtail=node
            #node.next=None
    return curhead,curtail

root=None
#nodelist=raw_input().split("-")
nodelist=['1','2','3','4','5','6','7','8']
#k=int(raw_input())
k=3
if nodelist:
    root=Node(nodelist[0])
else:
    raise(Exception("input invalid!"))
cur=root
for val in nodelist[1:]:
    cur.next=Node(val)
    cur=cur.next
head=transformLinkListByK(root,k)
arr=[]
cur=head
while cur:
    arr.append(cur.val)
    cur=cur.next
print "-".join(arr)

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值