avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

19.Delete the countdown of the linked listNNode

  2024-01-01        
字数统计: 386字   |   阅读时长: 2min

topic:

Give you a linked list,Delete the countdown of the linked list n Node,And return to the head point of the linked list。

 

Exemplary example 1:

enter:head = [1,2,3,4,5], n = 2
Output:[1,2,3,5]

Exemplary example 2:

enter:head = [1], n = 1
Output:[]

Exemplary example 3:

enter:head = [1,2], n = 1
Output:[1]

 

hint:

  • The number of nodes in the linked list is sz
  • 1 <= sz <= 30
  • 0 <= Node.val <= 100
  • 1 <= n <= sz

 

Advance:Can you try to use a scanning implementation??

Related Topics
  • Linked
  • Double pointer

  • 👍 2500
  • 👎 0
  • 19.Delete the countdown of the linked listNNode.md

    Thought:

    It is definitely possible to solve the problem by taking dual traversal,但topic要求我们一次遍历解决问题,Then we have to diverge on our ideas。

    我们可以设想假设设定了Double pointer p and q if,when q Pointed to the end NULL,p and q The number of elements separated from each other is n hour,Then delete p The next pointer completes the requirements。

    Set up virtual nodes dummyHead direction head
    设定Double pointer p and q,初始都direction虚拟节点 dummyHead
    move q,until p and q The number of elements separated from each other is n
    同hourmove p and q,until q direction的为 NULL
    Will p 的下一Nodedirection下下Node

    Code:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
    dummyHead = ListNode(val=0, next=head)
    head = tail = dummyHead
    for _ in range(n):
    tail = tail.next
    while tail.next is not None:
    head, tail = head.next, tail.next
    head.next = head.next.next
    return dummyHead.next
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    class Solution {
    public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
    ListNode *yummyNode = new ListNode(0);
    yummyNode->next = head;
    ListNode *fast = yummyNode;
    ListNode *slow = yummyNode;
    for(int i = 0; i < n; i++){
    slow = slow->next;
    }
    while (slow->next != nullptr){
    fast = fast->next;
    slow = slow->next;
    }
    //Recycling memory
    ListNode *del = fast->next;
    fast->next = del->next;
    delete del;
    //Return to node
    ListNode *ret = yummyNode->next;
    delete yummyNode;
    return ret;

    • Python
    • answer

    扫一扫,分享到微信

    微信分享二维码
    PythonCoincidence,Random algorithmO(nlogn)Push
    217. Existing duplicate elements C++/Python3
    目录
    1. 1. topic:
    2. 2. Thought:
    3. 3. Code:

    150 篇 | 131.7k
    次 | 人
    这里自动载入天数这里自动载入时分秒
    2022-2025 loong loong | 新南威尔士龙龙号