题解 | #反转链表#

反转链表

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;
    }
}
全部评论

相关推荐

大摆哥:刚好要做个聊天软件,直接让你帮他干活了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务