1. 程式人生 > >Remove Nth Node From End of List(刪除從最後一個結點起的第n個結點)

Remove Nth Node From End of List(刪除從最後一個結點起的第n個結點)

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

For 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.
Try to do this in one pass.                     
思路:第一種,把連結串列Reverse

,刪除第n個結點,然後在Reverse。第二種:求連結串列的長度lenth,然後刪除第(lenth-n)個結點

/**
 * 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) 
    {
        int lenth=0;
        int nth=0;
        ListNode dummyNode(0);
        dummyNode.next=head;
        ListNode *p=&dummyNode;
        while(p->next)
        {
            lenth++;
            p=p->next;
        }
        p=&dummyNode;
        nth=lenth-n;
        while(nth--&&p->next)
        {
            p=p->next;
        }
        p->next=p->next->next;
        return dummyNode.next;
    }
};

相關推薦

【LeetCode-面試演算法經典-Java實現】【019-Remove Nth Node From End of List移除單鏈表的倒數N節點

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

Remove Nth Node From End of List刪除最後一個結點n結點

Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4-&g

19. Remove Nth Node From End of List移除連結串列的倒數n節點

問題描述 Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->

LeetCode | Remove Nth Node From End of List移除連結串列中倒數n結點

題目: Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->

19.Remove Nth Node From End of List移除單鏈表中倒數N結點

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

LeetCode 19. Remove Nth Node From End of List刪除單鏈表倒數N結點

題目描述:    Given a linked list, remove the nth node from the end of list and return its head.例子: Give

leetcode:Remove Nth Node From End of List刪除連結串列倒數n節點【面試演算法題】

題目: Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->

【LeetCode】19. Remove Nth Node From End of ListC++

地址:https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 題目: Given a linked list, remove the

LeetCode 19. 刪除連結串列的倒數N節點 Remove Nth Node From End of ListC語言

題目描述: 給定一個連結串列,刪除連結串列的倒數第 n 個節點,並且返回連結串列的頭結點。 示例: 給定一個連結串列: 1->2->3->4->5, 和 n = 2. 當刪除了倒數第二個節點後,連結串列變為 1->2->3->

19. Remove Nth Node From End of List連結串列

https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ 題目:刪除倒數第n個節點(遍歷只能一趟) 思路:雙指標 例如:1->2->3->4->5, n =

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 =

LeetCode19. Remove Nth Node From End of ListC++

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, an

Remove Nth Node From End of List連結串列表尾刪除n節點

題目: Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3

【LeetCode題解】19_刪除連結串列的倒數N節點Remove-Nth-Node-From-End-of-List

更多 LeetCode 題解筆記可以訪問我的 github。 文章目錄 描述 解法:雙指標 思路 Java 實現 Python 實現 複雜度分析 描述 給定一個連結串列,

LeetCode 19. 刪除連結串列的倒數N節點Remove Nth Node From End Of List

題目描述 給定一個連結串列,刪除連結串列的倒數第 n 個節點,並且返回連結串列的頭結點。 示例: 給定一個連結串列: 1->2->3->4->5, 和 n = 2. 當刪除了倒數

【LeetCode & 劍指offer刷題】鏈表題4:22 刪除鏈表中倒數k結點19. Remove Nth Node From End of List

star urn n+1 valid ffffff 防禦性編程 normal move rgb 【LeetCode & 劍指offer 刷題筆記】目錄(持續更新中...) 19. Remove Nth Node From End of List Given a l

【LeetCode & 劍指offer刷題】連結串列題4:22 刪除連結串列中倒數k結點19. Remove Nth Node From End of List

【LeetCode & 劍指offer 刷題筆記】目錄(持續更新中...) 19. Remove Nth Node From End of List Given a linked list, remove the   n -th node from th

19. Remove Nth Node From End of List

while linked front cnblogs from ++ ava style solution https://leetcode.com/problemset/all/?search=19 涉及鏈表刪除操作的時候,穩妥起見都用 dummynode,省去很多麻煩。

leetcode 19. Remove Nth Node From End of List

刪除 else logs tco nth -1 move col n-1 Given a linked list, remove the nth node from the end of list and return its head. For example, G

19. Remove Nth Node From End of List 刪除倒數n各節點

得到 listnode 負責 style pre def 正數 always ast Given a linked list, remove the nth node from the end of list and return its head. For exampl