题解 | #反转链表#

反转链表

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

import java.util.*;

public class Solution {
    public ListNode ReverseList(ListNode head) {

        Stack<Integer> stack = new Stack<>();

        while (head != null) {
            stack.push(head.val);
            head = head.next;
        }

        if (stack.isEmpty()) {
            return null;
        }

        ListNode r =  new ListNode(stack.pop());

        ListNode p = r;

        while (!stack.isEmpty()) {
            r.next = new ListNode(stack.pop());
            r = r.next;
        }

        r.next = null;

        return p;
    }
}
全部评论

相关推荐

06-27 15:15
长安大学 Java
点赞 评论 收藏
分享
06-10 21:15
门头沟学院 Java
宁阿:好多这种没🧠的公司,他们估计都不知道毕业的人不能给安排实习岗
点赞 评论 收藏
分享
05-29 09:02
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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