Difficult:Medium
題目
Given the head of a linked list, remove the nth node from the end of the list and return its head.
翻譯
給定鍊錶的頭,從鍊錶的末尾刪除第 n 個節點並返回它的頭。
範例
Example 1
1 | Input: head = [1,2,3,4,5], n = 2 |
Example 2
1 | Input: head = [1], n = 1 |
Example 3
1 | Input: head = [1,2], n = 1 |
解題思路
1.先計算link list長度
2.設pre的node和cur的node
3.如果pre和cur都不為空時,將pre.next改為cur.next
4.如果pre為空和cur都不為空時,將head改為head.next
Solution
Code 1 :
1 | var removeNthFromEnd = function (head, n) { |