LintCode - 链表倒数第n个节点(普通)

版权声明:本文为博主原创文章,未经博主允许不得转载。

难度:容易
要求:

给定一个链表,删除链表中倒数第n个节点,返回链表的头节点。

样例

给出一个链表**1->2->3->null**,这个翻转后的链表为**3->2->1->null**

思路

/**
     * @param head: The head of linked list.
     * @return: The new head of reversed linked list.
     */
    public ListNode reverse(ListNode head) {
        ListNode p = null;
        while(head != null){
            ListNode tmp = head.next;
            head.next = p;
            p = head;
            head = tmp;
        }
        return p;
    }

你可能感兴趣的:(LintCode - 链表倒数第n个节点(普通))