leetcode19:Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.
這道題的要求是刪除連結串列的倒數第k個節點, 只要能查詢到倒數第k個節點,刪除啥的就好說了。
實現查詢不難,兩次遍歷連結串列,第一遍記錄連結串列長度,第二次直接遍歷到倒數第k個即可。難點在於如何設計高效的演算法一次遍歷連結串列實現查詢。
思路: 快慢指標。能否這樣想,倒數第k個就是從末尾往前數第k個,而最後一個節點是空指標。設計兩個指標,如果兩個指標的距離始終是k,那麼當後一個指標到尾節點時,前一個指標恰恰指向倒數第k個節點。這樣,大概思路就出來了,後指標先遍歷k個位置,然後前、後指標同步遍歷,當後指標為空時,就找到了倒數第k個位置的節點。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode p=head;//快(後)指標
ListNode pre=head;//前指標的前指標,刪除節點q使用
ListNode q=head.next;//慢(前)指標
for(int i=0;i<n;i++)
p=p.next;
if (p == null) //考慮邊界情況,刪除頭結點
{return pre.next;
//System.out.println("error n");
}
else if(p.next==null){//考慮邊界情況,刪除尾節點
pre.next=q.next;
return pre;
}
while(p.next!=null){
p=p.next;
q=q.next;
pre=pre.next;
}
pre.next=q.next;
return head;
}
}
結果:
連結串列:12345678
K:3
輸出:6
相關文章
- 19. Remove Nth Node From End of ListREM
- Leetcode Remove Nth Node From End of ListLeetCodeREM
- Leetcode 19 Remove Nth Node From End of ListLeetCodeREM
- Leetcode-Remove Nth Node From End of ListLeetCodeREM
- Remove Nth Node From End of List leetcode javaREMLeetCodeJava
- LeetCode Remove Nth Node From End of List(019)解法總結LeetCodeREM
- Leetcode(Python3) 19. Remove Nth Node From End of ListLeetCodePythonREM
- 資料結構與演算法 | Leetcode 19. Remove Nth Node From End of List資料結構演算法LeetCodeREM
- [LeetCode] Remove Nth Node From End of List 移除連結串列倒數第N個節點LeetCodeREM
- leetcode-019-刪除連結串列倒數第N個結點(Remove Nth Node From End of List)LeetCodeREM
- 【leetcode】19. 刪除連結串列的倒數第N個節點(remove-nth-node-from-end-of-list)(雙指標)[中等]LeetCodeREM指標
- Remove Duplicates from Sorted ListREM
- Remove-duplicates-from-sorted-listREM
- 83. Remove Duplicates from Sorted ListREM
- Leetcode Remove Duplicates from Sorted ListLeetCodeREM
- 82. Remove Duplicates from Sorted List IIREM
- Leetcode Remove Duplicates from Sorted List IILeetCodeREM
- Leetcode-Remove Duplicates from Sorted ListLeetCodeREM
- Remove Duplicates from Sorted List leetcode javaREMLeetCodeJava
- LeetCode 83. Remove Duplicates from Sorted ListLeetCodeREM
- 【Leetcode】83. Remove Duplicates from Sorted ListLeetCodeREM
- Leetcode-Remove Duplicates from Sorted List IILeetCodeREM
- Remove Duplicates from Sorted List II leetcode javaREMLeetCodeJava
- 【Leetcode】82. Remove Duplicates from Sorted List IILeetCodeREM
- [LeetCode] 2487. Remove Nodes From Linked ListLeetCodeREM
- 【LeetCode從零單排】No83 Remove Duplicates from Sorted ListLeetCodeREM
- Remove Duplicates from Sorted List 去除連結串列中重複值節點REM
- Remove a node from Oracle10g RAC cluster and add back for IBM AIXREMOracleIBMAI
- Remove-duplicates-from-sorted-arrayREM
- Remove Untagged Images From DockerREMDocker
- Java List的remove()方法陷阱JavaREM
- 26. Remove Duplicates from Sorted ArrayREM
- LeetCode 83.Remove Duplicates from Sorted List(從已排序連結串列中除去重複) Easy/Linked ListLeetCodeREM排序
- [CareerCup] 2.1 Remove Duplicates from Unsorted List 移除無序連結串列中的重複項REM
- 遍歷List 同時 remove 元素REM
- leetcode Remove Duplicates from Sorted ArrayLeetCodeREM
- for (auto it = _list.begin(); it != _list.end(); )關於在for迴圈中使用std::vector中的begin和end
- 【程式碼優化】List.remove() 剖析優化REM