code/leetcode/v1/removeend
Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head.
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.
Solution One
1 | class Solution { |
Pass one time and get the length of LinkedList, so just count length - n - 1
times we can find the pre pointer. All we need to do is point pre pointer.next equal the next of which pointer we should delete.
Solution Two
1 | class Solution { |
Just let the List structure give a help, we can transfer a LinkedList
to an Array
. So all we need to do is let list[list.length - n - 1].next
equals list[list.length - n].next
.
Base case: if delete the head node we just need to return head.next
.
Reference
https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list