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

2021字节跳动校招秋招算法面试真题解题报告--leetcode206反向链表,包含7种语言答案

时间:2023-04-01 17:49:13 Java

206.反向链表1.题目描述

反向链表。


示例:


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

进阶:
可以迭代或递归地反转链表。你能用两种方法解决这个问题吗?


2.解题报告思路一:利用栈利用栈先进后出的特点,将栈中的每个节点按顺序存放,然后将栈中的每个节点从上到下连接起来。将翻转后的最后一个节点(即原链表第一个节点)的next设置为nullptr,否则后果可想而知~思路二:原地操作(推荐)断开原链表每个节点一个byone(saveNextnode)将断开的节点连接到反向链表的表头更新反向链表的表头,返回原链表的下一个节点3.最佳答案cAnswer/***Definitionfor单链表.*structListNode{*intval;*结构列表节点*下一个;*};*///structListNode*reverseList(structListNode*head){//structListNode*new=NULL;//while(head){//structListNode*temp=head;//head=head->next;//temp->next=new;//new=temp;//}//returnnew;//}structListNode*reverseList(structListNode*head){if(!head||!head->next)返回头;结构ListNode*L=reverseList(head->next);头->下一个->下一个=头;head->next=NULL;returnL;}c++answer/***单向链表的定义。*结构列表节点{*intval;*列表节点*下一个;*ListNode(intx):val(x),next(NULL){}*};*/类解决方案{public:ListNode*reverseList(ListNode*head){if(!head||!head->next)returnhead;ListNode*node=reverseList(head->next);头->下一个->下一个=头;head->next=NULL;返回节点;}};java答案/***单向链表的定义。*公共类ListNode{*intval;*接下来是ListNode;*ListNode(intx){val=x;}*}*/classDIYStack{publicArrayListcontainer=newArrayList<>();publicvoidcomeIn(intitem){container.add(item);}publicintcomeOut(){返回container.remove(container.size()-1);}}classSolution{publicListNodereverseList(ListNodehead){DIYStackdiyStack=newDIYStack();ListNodetmp=head;while(tmp!=null){diyStack.comeIn(tmp.val);tmp=tmp.next;}tmp=头;while(tmp!=null){tmp.val=diyStack.comeOut();tmp=tmp.next;返回头;}}JavaScript答案/***单向链表的定义。*functionListNode(val){*this.val=val;*this.next=null;*}*//***@param{ListNode}head*@return{ListNode}*/varreverseList=function(head){varcur=head;varprev=null;while(cur){varnext=cur.next;当前.下一个=上一个;上一个=当前;当前=下一个;}returnprev;};c#答案/***单向链表的定义。*公共类ListNode{*publicintval;*接下来是公共ListNode;*publicListNode(intx){val=x;}*}*/publicclassSolution{publicListNodeReverseList(ListNodehead){ListNodeb=null;ListNode下一个索引;while(head!=null){Nextindex=head.next;head.next=b;b=头;head=下一个索引;}返回b;}}python2.x答案#单链表的定义。#classListNode(object):#def__init__(self,x):#self.val=x#self.next=NoneclassSolution(object):defreverseList(self,head):""":typehead:ListNode:rtype:ListNode"""如果head为None或head.next为None:returnheadstack=[]whilehead:stack.append(head)head=head.nextnewHead=stack[-1]#whilestack:#now=stack.pop()foriinrange(len(stack)-1,0,-1):stack[i].next=stack[i-1]stack[0].next=NonereturnnewHeadpython3.x答案#单向链表的定义。#classListNode:#def__init__(self,x):#self.val=x#self.next=Noneclasshead):""":typehead:ListNode:rtype:ListNode"""如果head为None:返回None如果head.next为None:returnheadp=headq=Nonewhilep:tmp=p.nextp.next=qq=pp=tmpreturnqgoanswer/***单向链表的定义。*typeListNodestruct{*Valint*Next*ListNode*}*/funcreverseList(head*ListNode)*ListNode{varpreNode*ListNode=nilvarcurrentNode*ListNode=headforcurrentNode!=nil{nextNode:=currentNode.Next当前节点。next=preNodepreNode=currentNodecurrentNode=nextNode}returnpreNode}4.leetcode解题报告合集leetcode最佳答案:leetcode是一个很好的练习算法能力和编程内功的平台,但是官网的解题报告不全,网上也有答案对错,为了提高大家提问的效率,将每道题的各种语言的答案(通过测试)汇总发布,供大家参考。每题包括c、c++、java、python、python3、javascript、8种常用语言答案、c#和go。