LeetCode 刷题记录 19. 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.

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值