题解 | #反转链表#

反转链表

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("}");
    }
}

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

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务