十一月才投了网易雷火的策划岗位,没下文了
点赞 3

相关推荐

09-15 11:05
已编辑
门头沟学院 Java
public class Solution {    public ListNode reverseBetween(ListNode head, int m, int n) {if(head==null)return null;ListNode preHead=new ListNode(0);preHead.next=head;ListNode  head0=head;ListNode head1= head;ListNode tempList=null;ListNode node1=null,node2=null,next=null,pre=null;//当节点为第一个节点时if(m==1)pre=preHead;//循环找到指定的节点的前一个节点for(int i=0; i<m-1; i++){if(i==0)pre=preHead;pre=pre.next;}//找到要反转节点的位置node1=pre.next;ListNode temp=node1;//循环找到要反转的链表的最后一个节点for(int i=0; i<n-m; i++){node2=temp.next;temp= temp.next;}// 分类讨论,当节点m=n时候if(m==n){node2=temp;next=node2.next;node2.next=null;}// m!=n时else{next=node2.next;node2.next=null;}// 调用第一题反转链表的函数tempList=run(node1);// 将前驱节点指向反转链表首部pre.next=tempList;// 找到反转链表的最后一个元素while(tempList.next!=null){tempList=tempList.next;}// 将将反转链表的最后一个元素指向原链表位置的下一个元素tempList.next=next;//返回头节点return  preHead.next;}//反转链表函数public static ListNode run(ListNode head){  if(head==null)            return null;            ListNode reversedHead=null;            ListNode current=head;            ListNode tmp=null;                    while(current!=null){            tmp= current;            current= current.next;            tmp.next=null;            if(reversedHead==null)            reversedHead=tmp;            else{                tmp.next=reversedHead;                reversedHead=tmp;            }                 }            return reversedHead;     }}
投递思源智通等公司10个岗位
点赞 评论 收藏
分享
牛客网
牛客企业服务