1. 程式人生 > >19. Remove Nth Node From End of List--python

19. Remove Nth Node From End of List--python

題目:

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?

解題

利用的是fast和slow雙指標來解決。首先先讓fast從起始點往後跑n步。然後再讓slow和fast一起跑,直到fast.next為null時候,slow所指向的node就是需要刪除節點的前面的節點。
圖解:
這裡寫圖片描述

# 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 """ fast = slow = head for _ in range(n): fast = fast.next if not fast: return head.next while fast.next: fast = fast.next slow = slow.next slow.next = slow.next.next return
head

相關推薦

19. Remove Nth Node From End of List--python

題目: Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->

[leetcode] 19. Remove Nth Node From End of List python實現【easy】

Remove Nth Node From End of List My Submissions QuestionEditorial Solution Given a linked list, remove the nth node from

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

【leetcode】19. Remove Nth Node From End of List

log become 兩個 str note count con not one 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

ber 指針 lang bsp for style Language 示例 opera 給定一個鏈表,刪除鏈表的倒數第 n 個節點,並且返回鏈表的頭結點。 示例: 給定一個鏈表: 1->2->3->4->5, 和 n = 2. 當刪除了倒數第二個節

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-&

【Leetcode】【連結串列】 19. Remove Nth Node From End of List / 刪除連結串列的倒數第N個節點】

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

[leetcode] 19. Remove Nth Node From End of List (Medium)

原題連結 刪除單向連結串列的倒數第n個結點。 思路: 用兩個索引一前一後,同時遍歷,當後一個索引值為null時,此時前一個索引表示的節點即為要刪除的節點。 Runtime: 13 ms, faster than 24.49% of Java class Solution { public Lis

【LeetCode】19. Remove Nth Node From End of List - Java實現

文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Given a linked list, remove the n-th node from the end of list and retur

19 Remove Nth Node From End of List

別想花裡胡哨的了。。倒置刪除再倒置吧,複雜度也就n,就是程式碼長了點 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; *

19. Remove Nth Node From End of List C++

使用雙指標法,可以僅遍歷一次完成節點的定位 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : va

【LeetCode】19. Remove Nth Node From End of List(C++)

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

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

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 =

[LeetCode] 19. Remove Nth Node From End of List 刪除連結串列的倒數第N個節點 @python

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

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 & 劍指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