题解 | #反转链表#

反转链表

https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca

import java.util.*;

public class Main {
    public static class ListNode {
        private int val;
        private ListNode next = null;
        public ListNode(int val) {
            this.val = val;
        }
    
        public void setVal(int val){
            this.val = val;
        }

        public int getVal(){
            return this.val;
        }

        public void setNext(ListNode next){
            this.next = next;
        }

        public ListNode getNext(){
            return this.next;
        }
    } 
    
    public static ListNode reverseList(ListNode head) {
        List<Integer> valList = new ArrayList<>();
        ListNode currNode = head;
        while(currNode!=null){
            valList.add(currNode.getVal());
            currNode = currNode.getNext();
        }
        int revesedVal =valList.get(valList.size()-1);
        ListNode revsedHead = new ListNode(revesedVal);
        ListNode curr_node = null;
        for(int i=valList.size()-1; i>=0; i--){
            int val = valList.get(i);
            if(i==valList.size()-1){
                int nextVal = valList.get(i-1);
                curr_node = revsedHead;
                ListNode nextNode = new ListNode(nextVal);
                curr_node.setNext(nextNode);
            } else if(i==0){
                curr_node = curr_node.getNext();
                curr_node.setNext(null);
            } else {
                curr_node = curr_node.getNext();
                int nextVal = valList.get(i-1);
                ListNode nextNode = new ListNode(nextVal);
                curr_node.setNext(nextNode);
            }
        }
        return revsedHead;
    }
    
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();
        String numStr = line.substring(1, line.length()-1);
        if("".equals(numStr) || numStr==null || numStr.length()==1){
            System.out.println(line);
            return;
        }
        String[] numArray = numStr.split(",");
        ListNode head = null;
        ListNode currNode = null;
        for(int i=0; i<numArray.length; i++){
            int val = Integer.parseInt(numArray[i]);
            if(i==0){
                head = new ListNode(val);
                currNode = head;
                int nextVal = Integer.parseInt(numArray[i+1]);
                ListNode nextNode = new ListNode(nextVal);
                currNode.setNext(nextNode);
            } else if(i==numArray.length-1){
                currNode = currNode.getNext();
                currNode.setNext(null);
            } else {
                currNode = currNode.getNext();
                int nextVal = Integer.parseInt(numArray[i+1]);
                ListNode nextNode = new ListNode(nextVal);
                currNode.setNext(nextNode); 
            }
        }
        ListNode revesedNode = reverseList(head);
        System.out.print("{");
        ListNode currList = revesedNode;
        while(currList!=null){
            System.out.print(currList.getVal());
            if(currList.getNext()!=null){
                System.out.print(",");
            }
            currList = currList.getNext();
        }
        System.out.print("}");
    }
}

#牛客求职必刷题#
全部评论

相关推荐

刷到过很多实习生和mentor处成哥们儿的帖子,看完大概率会羡慕这种亲近的上下级关系。但我想说,不管你跟带教的关系有多铁或者多僵,都得拎清课题分离这事儿,工作的边界感千万不能乱踩!📝&nbsp;下面这几条雷区,实习生们可千万别踩!1.别越级瞎提建议除非带教主动问你想法,否则别愣头愣脑地凑上去说&nbsp;“我觉得你应该咋样咋样”。咱就管好自己那一亩三分地,把本职工作干明白就够了,别瞎操心不属于自己的事儿。2.别卖队友换好感❌那种把实习生之间的小秘密捅出去,就为了换带教或者同事认可的操作,纯纯是作死。尤其是那些会影响到别人的话,比如&nbsp;“她就是嫌通勤远才请假”“他们偷偷建了个小群吐槽”,这话一出口,不仅把同事的信任踩碎了,自己还得惹一身麻烦。3.别养成反驳甩锅的臭毛病📗犯了错第一反应别是找借口、甩锅,那没啥用。不如沉下心复盘问题出在哪儿,琢磨个解决方案出来,保证下次不再掉坑里,这才是正经事。4.别瞎掺和正职的八卦我之前实习的组氛围贼好,正职们经常聊对象、聊恋爱进展啥的。但人家正职能聊,不代表咱实习生能跟着掺和。乖乖当个安静的吃瓜群众,竖起耳朵听着就行,别瞎插嘴,当个小透明准没错👂4.别随便评价别人的人品背后嚼舌根说别人咋样咋样,很容易让人觉得你没分寸。你聊啥,别人就可能觉得你认同啥,职场里保持中立,少议论人,才是最稳的。
omega@1112:唯一的大雷其实是不要问mentor的薪资待遇
mt对你说过最有启发的一...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务