Lintcode LinkedList 174 Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head.

Example

Given linked list: 1->2->3->4->5->null, and n = 2.

After removing the second node from the end, the linked list becomes 1->2->3->5->null.


C++:

/**
 * Definition of ListNode
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *         this->val = val;
 *         this->next = NULL;
 *     }
 * }
 */
class Solution {
public:
    /**
     * @param head: The first node of linked list.
     * @param n: An integer.
     * @return: The head of linked list.
     */
    ListNode *removeNthFromEnd(ListNode *head, int n) {
        // write your code here
        if (!head->next) return NULL;
        ListNode *pre = head, *cur = head;
        for (int i = 0; i < n ; ++i) cur = cur->next;
        if (!cur) return head->next; 不用dummy
        while (cur->next) {
            cur = cur->next;
            pre = pre->next;
        }
        pre->next = pre->next->next;
        return head;
    }
};

C++ dummy版本
/**
 * Definition of ListNode
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *         this->val = val;
 *         this->next = NULL;
 *     }
 * }
 */
class Solution {
public:
    /**
     * @param head: The first node of linked list.
     * @param n: An integer.
     * @return: The head of linked list.
     */
    ListNode *removeNthFromEnd(ListNode *head, int n) {
        // write your code here
        ListNode *dummy = new ListNode(0);
        dummy->next = head;
        ListNode *pre = dummy;
        
        for(int i=0;i<n;i++){
            if(head==NULL)
            return NULL;
            head = head->next;
        }
        
        while(head!=NULL){
            head = head->next;
            pre = pre->next;
        }
        
        pre->next = pre->next->next;
        return dummy->next;
    }
};



JAVA:
创建dummy一个值为0的头指针,将head连接到dummy后,如果Listnode head 为1->2->3->4->5->null,连接后的dummy为
0->1->2->3->4->5->null。注意head指针仍指向值为1的(即第二个)结点,引进dummy的原因是在1->2->null,n=2这种情况
无需删除头结点,返回dummy.next即可解决。

head指向dummy的第二个结点,i<n-1,head循环之后指向第三个结点,pre指向第一个结点,0->1->2->3->4->5->null。
只要head.next不是null,head和pre就向后同步移动,0->1->2->3->4->5->null。然后用
pre.next = pre.next.next删除结点值为4的结点。最后返回dummy.next将值为0的结点去掉。

如果for循环中i<n,那么while循环中的条件改为head!=null,
0->1->2->3->4->5->null同步移动到0->1->2->3->4->5->null

/**
 * Definition for ListNode.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int val) {
 *         this.val = val;
 *         this.next = null;
 *     }
 * }
 */ 
public class Solution {
    /**
     * @param head: The first node of linked list.
     * @param n: An integer.
     * @return: The head of linked list.
     */
    ListNode removeNthFromEnd(ListNode head, int n) {
        // write your code hereif (n <= 0) {
        if(n <= 0) return null;
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode pre = dummy;
        for(int i = 0; i<n-1; i++){
            if(head == null)
            return null;
            head = head.next;
        }
        
        while(head.next!=null){
            head = head.next;
            pre = pre.next;
        }
        
        pre.next = pre.next.next;
        return dummy.next;  
    }
}


Python
"""
Definition of ListNode
class ListNode(object):

    def __init__(self, val, next=None):
        self.val = val
        self.next = next
"""
class Solution:
    """
    @param head: The first node of linked list.
    @param n: An integer.
    @return: The head of linked list.
    """
    def removeNthFromEnd(self, head, n):
        # write your code here
        res = ListNode(0)
        res.next = head
        tmp = res
        for i in range(0, n):
            head = head.next
        while head != None:
            head = head.next
            tmp = tmp.next
        tmp.next = tmp.next.next
        return res.next

类似的思路还有LinkedList166
Nth to Last Node in List,只需找到结点,无需返回链表,所以无需构造dummy。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值