题目:
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.
Follow up:
Could you do this in one pass?
解法1:
由于要求一趟解决,所以要使用快慢指针,首先让快指针走n步,然后快慢指针一块走,快指针指向最后一个元素时停止,此时慢指针指向要删除的元素的前面,改变指针以达到删除元素的目的
另外该解法不增加头结点,所以要考虑删除元素是头元素的情况,此时慢指针无法指向前一个元素,故要分情况讨论,如果快指针走了n步后指向空,则直接返回head -> next,否则为head
c++:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* fast = head;
ListNode* slow = head;
while(n--){
fast = fast -> next;
}
if(fast == nullptr) return head -> next;
while(fast -> next){
fast = fast -> next;
slow = slow -> next;
}
ListNode* temp = slow -> next;
slow -> next = slow -> next -> next;
delete temp;
return head;
}
};
java:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head == null){
return null;
}
ListNode fast = head, slow = head;
for(int i = 0; i < n; i++){
fast = fast.next;
}
if(fast == null) return head.next;
while(fast.next != null){
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return head;
}
}
python:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
if head is None:
return None
fast = slow = head
for i in range(n):
fast = fast.next
if fast is None: return head.next
while fast.next != None:
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return head
解法2:
与解法1相同,只不过加入头结点,不用分情况讨论
c++:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
if(!head){
return nullptr;
}
ListNode dummy(-1);
dummy.next = head;
ListNode* fast = &dummy, *slow = &dummy;
while(n--){
fast = fast -> next;
}
while(fast -> next){
fast = fast -> next;
slow = slow -> next;
}
ListNode* temp = slow -> next;
slow -> next = slow -> next -> next;
delete temp;
return dummy.next;
}
};
java:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head == null){
return null;
}
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode fast = dummy, slow = dummy;
for(int i = 0; i < n; i++){
fast = fast.next;
}
while(fast.next != null){
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummy.next;
}
}
Python:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
if head is None:
return None
dummy = ListNode(-1)
dummy.next = head
fast = slow = dummy
for i in range(n):
fast = fast.next
while fast.next != None:
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return dummy.next