Nth to Last Node in List
2015-07-06 21:13
507 查看
题目描述
链接地址
解法
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.
链接地址
解法
题目描述
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) { // write your code here if (head == NULL || n < 0) { // input is error return head; } ListNode *first = head; ListNode *second = head; 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; }
相关文章推荐
- 20150624 lintcode 总结 binary-tree-level-order-traversal **
- 20150624 lintcode 总结 Lowest Common Ancestor
- 20150624 lintcode 总结 Balanced Binary Tree
- Lintcode(1)-两个字符串是变位词
- Lintcode(2)-比较字符串
- Lintcode(3)-字符串查找
- Lintcode(4)-乱序字符串
- Lintcode(5)-最长公共子串
- Lintcode(6)-最长公共前缀
- Lintcode(7)-删除元素
- #LintCode# Binary Search
- LintCode刷题之路(一)
- 爬楼梯
- LintCode刷题链表排序——归并和快速排序
- 克隆二叉树
- 旋转图像
- 图中两个点之间的路线
- 丑数 II
- 丑数
- 落单的数 III