题解 | #反转链表#
反转链表
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ import java.util.*; public class Solution { public ListNode ReverseList(ListNode head) { if(head==null) return head; Stack<ListNode> stack = new Stack<ListNode>(); stack.push(head); ListNode node = head; while(node.next!=null){ stack.push(node.next); node=node.next; } ListNode newhead = stack.pop(); ListNode currentNode = newhead; while(stack.size()>0){ currentNode.next=stack.pop(); currentNode=currentNode.next; } currentNode.next=null; return newhead; } }