反转链表

反转链表

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

/*
public class ListNode {
int val;
ListNode next = null;

ListNode(int val) {
    this.val = val;
}

}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head == null || head.next == null) {
return head;
}
// 递归到最后节点
ListNode last = ReverseList(head.next);
// 最后节点指向倒数第二个节点head
head.next.next = head;
// 不需要指向最后节点了
head.next = null;
return last;
}
}

全部评论

相关推荐

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