翻转链表

反转链表

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

题目:https://www.nowcoder.com/questionTerminal/75e878df47f24fdc9dc3e400ec6058ca

//用递归的思路,保留头指针即可
public class Solution {

public ListNode ReverseList(ListNode head) {
    return reverse(null, head);
}

ListNode temp;
ListNode first;
public ListNode reverse(ListNode last, ListNode head) {
    if(head != null) {
        reverse(head, head.next);
        head.next = last;
        temp = head;
    }else {
        first = last;
    }
    return first;
}

}

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

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