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.
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.
翻譯:
給你一個連結串列,移除倒數第N個節點。
思路:
這道題難度還行,就是一些細節的方面,首先可能連結串列就一個節點。其次有可能被刪除的是第一個節點。最後就是刪除該節點後節點的連線。
程式碼:
我採用的辦法是兩個指標p,q,q先遍歷到n-1的位置,然後兩個指標同時向後遍歷,直到q到結尾,此時p為應該刪去的。同時來個指標指向p的前一個。public ListNode removeNthFromEnd(ListNode head, int n) { if(head==null||(head.next == null && n ==1)) return null; ListNode p = head; ListNode q = head; ListNode pre = head; while(n!=1) { q = q.next; n--; } while(q.next!=null) { pre = p; q = q.next; p = p.next; } if(pre.next == p.next) head = head.next; else pre.next = p.next; return head; }
如果p 和pre 指向的是同一個節點,說明此時刪去的是 第一個元素。因為上一部q.next==null,這是隻要把頭結點指向他下一個節點即可。
如果p和pre不等,則直接刪除p
相關推薦
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. For example, Given linked list: 1->2->
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
leetcode#19 Remove Nth Node From End of List
ber 指針 lang bsp for style Language 示例 opera 給定一個鏈表,刪除鏈表的倒數第 n 個節點,並且返回鏈表的頭結點。 示例: 給定一個鏈表: 1->2->3->4->5, 和 n = 2. 當刪除了倒數第二個節
[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
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given
[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]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刪除鏈表的倒數第N個節點
指針 ext 但是 res bec pan [1] note urn Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l
[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
LeetCode-19-Remove Nth Node From End of List
lin move valid list head agg ptr 兩個指針 fast 算法描述: Given a linked list, remove the n-th node from the end of list and return its head. Ex
LeetCode(19) Remove Nth Node From End of List
題目如下: 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-&g
[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->3->4->5, and n
【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 / 刪除連結串列的倒數第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 - Java實現
文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Given a linked list, remove the n-th node from the end of list and retur
[leetcode]19. Remove Nth Node From End of Liste
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } *
【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 & 劍指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