Leetcode 206. Reverse Linked List

206. Reverse Linked List Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both? 分析 最简单的方式,通过Stack来入栈出栈操作实现。 Java代码实现 class Solution { public ListNode reverseList(ListNode head) { if (head == null || head.next == null) { return head; } Stack<ListNode> stack = new Stack<>(); while (head != null) { stack.push(head); head = head.next; } ListNode tempNode = stack....

1 min · 133 words · Me