当前位置: 首页 > 后端技术 > Java

2021字节跳动秋季招聘算法面试热门真题,含7种语言答案_1

时间:2023-04-01 14:12:06 Java

链表排序1.题目描述

以O(nlogn)时间复杂度和常量级空间复杂度对链表进行排序。


例1:


输入:4->2->1->3
输出:1->2->3->4

示例2:


输入:-1->5->3->4->0
输出:-1->0->3->4->5

canswer/***单链表的定义。*结构列表节点{*intval;*结构列表节点*下一个;*};*///head不参与排序!voidmergeSortList(structListNode*head){structListNode*slow,*fast,*pre;如果(NULL==头)返回;//无效输入else{structListNodeR;慢=快=头;做{if((fast=fast->next)!=NULL){fast=fast->next;慢=慢->下一步;}}而(快);如果(NULL==slow->next)返回;//只有0或1个元素R.next=slow->next;慢->下一个=NULL;//拆分成两个mergeSortList(head);//使左半部分有序mergeSortList(&R);//让右半边有序slow=head->next;快速=R.next;//双方准备合并}pre=head;//尾部插入方式(slow的前身)do{if(fast->valval){//右边比左边小pre=pre->next=fast;//原来pre->next==slow,所以链不会丢fast=fast->next;pre->next=慢;//恢复pre是slow的前身if(fast==NULL)break;//左边的pre本来是跟着slow的,不用处理}else{//小的左边pre=slow;慢=慢->下一步;if(slow==NULL){pre->next=fast;//剩下的右边连接到左边的break;}}}while(true);}structListNode*sortList(structListNode*head){structListNodeHead;Head.next=头;//真正的头节点,不参与排序mergeSortList(&Head);returnHead.next;}c++answer/***单向链表的定义。*结构列表tNode{*整数值;*列表节点*下一个;*ListNode(intx):val(x),next(NULL){}*};*/classSolution{public:ListNode*merge(ListNode*left,ListNode*right){ListNode*newhead=NULL,*pre=NULL;while(left&&right){if(left->valval){if(!newhead){newhead=pre=left;左=左->下一个;}else{pre->next=left;pre=pre->next;左=左->下一个;}}else{if(!newhead){newhead=pre=right;右=右->下一个;}else{pre->next=right;pre=pre->next;右=右->下一个;}}}while(left){pre->next=left;左=左->下一个;pre=pre->next;}while(right){pre->next=right;右=右->下一个;pre=pre->next;}pre->next=NULL;返回新头;}ListNode*sortList(ListNode*head){if(head==NULL||head->next==NULL)returnhead;else{ListNode*slow=head,*fast=head->next;while(fast!=NULL&&fast->next!=NULL){slow=slow->next;fast=fast->next->next;}ListNode*temp=slow->next;慢->下一个=NULL;ListNode*l=sortList(head);ListNode*r=sortList(temp);返回合并(l,r);}}};javaanswer/***单向链表的定义。*公共类ListNode{*intval;*接下来是ListNode;*ListNode(intx){val=x;}*}*/classSolution{publicListNodesortList(ListNodehead){if(head==null||head.next==null)returnhead;ListNodefast=head;ListNodeslow=head;//快慢指针寻找链表的中间节点while(fast.next!=null&&fast.next.next!=null){fast=fast.next.next;慢=慢.next;}//将链??表分成两部分ListNodel1=head;ListNodel2=slow.next;慢.next=null;//合并排序l1=sortList(l1);l2=排序列表(l2);返回合并(l1,l2);}//合并两个排序列表privateListNodemerge(ListNodel1,ListNodel2){if(l1==null)returnl2;如果(l2==null)返回l1;ListNodedummy=newListNode(-1);ListNodecur=dummy;while(l1!=null&&l2!=null){if(l1.val<=l2.val){cur.next=l1;l1=l1.下一个;}else{cur.next=l2;l2=l2.下一个;}cur=cur.next;}if(l1!=null)cur.next=l1;否则cur.next=l2;返回dummy.next;}}JavaScript答案/***单向链表的定义。*functionListNode(val){*this.val=val;*this.next=null;*}*//***@param{ListNode}head*@return{ListNode}*/varmerge=function(l1,l2){varl=newListNode();变量p=l;while(l1&&l2){if(l1.vallist=newList();while(now!=null){list.Add(now.val);现在=现在.下一个;}list.Sort();intlength=list.Count;ListNoderesult=newListNode(list[0]);现在=结果;对于(inti=1;i<长度;i++){now.next=newListNode(list[i]);现在=现在.下一个;}返回结果;}}python2.x答案#单链表的定义。#classListNode(object):#def__init__(self,x):#self.val=x#self.next=NoneclassSolution(object):defsortList(self,head):""":typehead:ListNode:rtype:ListNode"""node=headres=[]whilenode:res.append(node.val)node=node.nextres.sort()如果不是res:returnNonenewhead=ListNode(res[0])cur_Node=newheadfori,vinenumerate(res):ifi==0:continueobj=ListNode(v)cur_Node.next=objcur_Node=objreturnnewheadpython3.x答案#单链表的定义。#classListNode:#def__init__(self,x):#self.val=x#self.next=Noneclass解决方案:defsortList(self,head):""":typehead:ListNode:rtype:ListNode"""ifnothead:returnNoneres=[]whilehead:res.append(head)head=head.nextres.sort(key=lambdaele:ele.val)temp=res[0]foriinrange(1,len(res)):temp.next=res[i]temp=res[i]res[-1].next=Nonereturnres[0]go答案/***单向链表的定义。*typeListNodestruct{*Valint*Next*ListNode*}*/funcsortList(head*ListNode)*ListNode{ifhead==nil||head.Next==nil{returnhead}middle:=split(head)returnmerge(sortList(head),sortList(middle))}//双指针切分listfuncsplit(head*ListNode)*ListNode{slow,fast:=head,headvartail*ListNodeforfast!=nil&&fast.Next!=nil{tail=slowslow=slow.Nextfast=fast.Next.Next}tail.Next=nilreturnslow}funcmerge(left,right*ListNode)*ListNode{varhead,cur,pre*ListNodeforleft!=nil&&right!=}nil{ifleft.Val