**一. 题目描述**
Given a linked list, remove the n th node from the end of list and return its head.
For example, Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
• Given n will always be valid.
• Try to do this in one pass.
**二. 题目分析**
给出一个链表, `n`是指删除**倒数**第`n`个节点。这里的提示`n`的值默认是合法的。不过其实对输入的n进行异常判断也只需要几句语句。
使用两个指针,即快/慢指针的概念,其中一个指针先走`n`步,然后慢指针走,等到快指针走到结尾时,那么慢指针走到了需要删除的节点的**前一个位置**。
这道题主要难点是考虑边界问题,以及特殊情况(要删除的是头节点),如输入`1->2->3->4`, `n=4`,那么需要删除`1`,此时只需将头指针`head = head->next`就可以了。
**三. 示例代码**
~~~
#include <iostream>
struct ListNode
{
int value;
ListNode* next;
ListNode(int x): value(x), next(NULL){};
};
class Solution
{
public:
ListNode *removeNthFromEnd(ListNode *head, int n)
{
if (head == NULL)
return NULL;
ListNode *fast = head;
ListNode *slow = head;
ListNode *temp = head;
for (int i = 0; i < n ; i++)
{
fast = fast->next;
if (fast)
continue;
else break;
}
while (fast)
{
fast = fast->next;
temp = slow;
slow = slow->next;
}
if (slow == head)
{
head = head->next;
return head;
}
temp->next = slow->next;
delete slow;
return head;
}
};
~~~
结果:
![](https://box.kancloud.cn/2016-01-05_568bb5ec718db.jpg)
![](https://box.kancloud.cn/2016-01-05_568bb5ed7faaa.jpg)
![](https://box.kancloud.cn/2016-01-05_568bb5ed9156c.jpg)
**四. 小结**
实际编程中经常会遇到边界问题,不小心的错误很容易造成程序奔溃,关于指针和链表的使用技巧还需要进一步的学习。
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016腾讯软件开发笔试题)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契数列问题)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波兰式的计算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和问题)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching