Nth to Last Node in List

题目描述

Find the nth to last element of a singly linked list.

The minimum number of nodes in list is n.
Example

Given a List 3->2->1->5->null and n = 2, return node whose value is 1.

链接地址

http://www.lintcode.com/en/problem/nth-to-last-node-in-list/

解法

     ListNode *nthToLast(ListNode *head, int n) {
if (head == NULL || n < 0) {
// input is error
}
int i;
for ( i = 0; i < n && second != NULL; i++) {
second = second->next;
}
if (second == NULL && i < n) {
// Input is error
return NULL;
}
while (second != NULL) {
first = first->next;
second = second->next;
}
return first;
}
• 本文已收录于以下专栏：

【LeetCode-面试算法经典-Java实现】【019-Remove Nth Node From End of List（移除单链表的倒数第N个节点）】

【019-Remove Nth Node From End of List（移除单链表的倒数第N个节点）】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题　　Given a l...

[C++]LeetCode 19: Remove Nth Node From End of List（删除链表中倒数第n个节点）

Problem： Given a linked list, remove the nth node from the end of list and return its head. For exam...

[LeetCode] 019. Remove Nth Node From End of List (Easy) (C++/Python)

[LeetCode] 019. Remove Nth Node From End of List (Easy) (C++/Python)
• hcbbt
• 2015年03月04日 19:26
• 2137

leetcode:单链表之Remove Nth Node From End of List

leetcode:单链表之Remove Nth Node From End of List 题目: Given a linked list, remove the nth node from the ...

leetcode-Remove Nth Node From End of List

举报原因： 您举报文章：Nth to Last Node in List 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)