Leetcode(Python3) 19. Remove Nth Node From End of List
The question is :Given the head of a linked list, remove the nth node from the end of the list and return its head.
A follow-up question: Could you do this in one pass?
Example input and output:
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
題目要求刪除連結串列倒數第n個結點,並返回刪除後的連結串列。這個練習可以幫助理解連結串列的指標到底是怎麼工作的。
先放三段通過測試的程式碼,第二段是我照著第一段寫的,第一段和第三段是其他人的解法。之後會再補充一點註釋。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
v_head = ListNode() #建立空結點
v_head.next = head #指向第一個結點
p = q = v_head #p,q兩個頭結點
for _ in range(n):
q = q.next
while q.next:
p = p.next
q = q.next
p.next = p.next.next
return v_head.next
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
v_head = ListNode()
v_head.next = head
sz = 0
p = q = v_head
while p.next:
p = p.next
sz += 1
i = 0
while i <= sz - n - 1:
q = q.next
i += 1
q.next = q.next.next
return v_head.next
class Solution(object):
def removeNthFromEnd(self, head, n):
# traverse for the first time and get the location(k) of the node we want to delete
countNode = head #let countNode point to the same node as head
numNodes = 0
while countNode is not None:
countNode = countNode.next
numNodes += 1
k = numNodes - n
#create two pointers prev and ptr, prev is always 1-lag behind ptr, after traversing again, ptr point to the node we want to delete, prev to the previous node
prev = None
ptr = head
while k != 0:
prev = ptr
ptr = ptr.next
k -= 1
# A solution without a new head node, so inserting the first node and the other nodes is different
if prev is None:
return head.next
else:
prev.next = ptr.next
ptr.next = None
return head
相關文章
- Leetcode 19 Remove Nth Node From End of ListLeetCodeREM
- LeetCode Remove Nth Node From End of List(019)解法總結LeetCodeREM
- 資料結構與演算法 | Leetcode 19. Remove Nth Node From End of List資料結構演算法LeetCodeREM
- 【leetcode】19. 刪除連結串列的倒數第N個節點(remove-nth-node-from-end-of-list)(雙指標)[中等]LeetCodeREM指標
- leetcode-019-刪除連結串列倒數第N個結點(Remove Nth Node From End of List)LeetCodeREM
- [LeetCode] 2487. Remove Nodes From Linked ListLeetCodeREM
- LeetCode 83. Remove Duplicates from Sorted ListLeetCodeREM
- Remove-duplicates-from-sorted-listREM
- Leetcode 26 Remove Duplicates from Sorted ArrayLeetCodeREM
- Leetcode 203. Remove Linked List ElementsLeetCodeREM
- LeetCode | 203. Remove Linked List ElementsLeetCodeREM
- [leetcode]remove-duplicates-from-sorted-array-iiLeetCodeREM
- LeetCode 83.Remove Duplicates from Sorted List(從已排序連結串列中除去重複) Easy/Linked ListLeetCodeREM排序
- [LeetCode] 80. Remove Duplicates from Sorted Array IILeetCodeREM
- LeetCode 382 Linked List Random NodeLeetCoderandom
- Leetcode 237. Delete Node in a Linked ListLeetCodedelete
- where to start, from where the end
- Remove-duplicates-from-sorted-arrayREM
- Java List的remove()方法陷阱JavaREM
- [leetcode]remove-elementLeetCodeREM
- 【leetcode】26. Remove Duplicates from Sorted Array 刪除有序陣列的重複元素LeetCodeREM陣列
- leetcode-27. Remove ElementLeetCodeREM
- Leetcode 27 Remove-ElementLeetCodeREM
- [LintCode/LeetCode] Remove Duplicate LettersLeetCodeREM
- 【程式碼優化】List.remove() 剖析優化REM
- for (auto it = _list.begin(); it != _list.end(); )關於在for迴圈中使用std::vector中的begin和end
- [LeetCode] 402. Remove K DigitsLeetCodeREMGit
- [Javascript] Find Items from the end of the JavaScript Array using at, findLast and findLastIndexJavaScriptASTIndex
- SciTech-Mathmatics-ImageProcessing-Remove the Background from an image using Python?REMPython
- [LeetCode] 1545. Find Kth Bit in Nth Binary StringLeetCode
- Leetcode Remove Duplicates型別題目 (python)LeetCodeREM型別Python
- MySQL報錯Slave: received end packet from server, apparent master shutdownMySqlServerAPPAST
- 【leetcode】LCP 19. 秋葉收藏集(UlBDOe)(DP)[中等]LeetCode
- node中的response.write()和response.end()
- [LeetCode] 61. Rotate ListLeetCode
- [LeetCode] 148. Sort ListLeetCode
- LeetCode | 148. Sort ListLeetCode
- LeetCode | 141 linked list cycleLeetCode