LinkedList Questions: Delete a given node in constant time

In this series of posts, I will discuss coding questions on the LinkedList Data structure.
The posts in this series will be organized in the following way,

Question Link ❓
Possible Explanation ?
Documented C++ Code ?
Time and Space Complexity Analys…


This content originally appeared on DEV Community and was authored by Kathan Vakharia

In this series of posts, I will discuss coding questions on the LinkedList Data structure.
The posts in this series will be organized in the following way,

  1. Question Link ❓
  2. Possible Explanation ?
  3. Documented C++ Code ?
  4. Time and Space Complexity Analysis ⌛?

The Question

Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list, instead you will be given access to the node to be deleted directly.

It is guaranteed that the node to be deleted is not a tail node in the list.

https://leetcode.com/problems/delete-node-in-a-linked-list/

? Give yourself atleast 5 mins to figure out the solution :)

Explanation

image

It is a warm-up question, there's not much to explain,

  1. Copy the data of the next node into the given node.
  2. Make the given node point to the next's neighbor.
  3. Free up the old neighbor of the given node.

C++ Code

Definition of LinkedList

//Definition for singly-linked list.
struct ListNode
{
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

Solution

#include <bits/stdc++.h>
#include "../linkedlist.h"
using namespace std;

/*
-Time:O(1)
-Space:O(1)
*/
class Solution
{
public:
    //! given node isn't the "tail" node
    void deleteNode(ListNode *node)
    {
        ListNode *temp = node->next;

        //I can do this only because node isn't the tail node
        //and "node->next" will not null therefore
        node->val = node->next->val;
        node->next = node->next->next;

        delete temp;
    }
};

Complexity Analysis

Time Complexity: O(1)

No iteration or recursion is done.

Space Complexity: O(1)

No extra space is used.


This content originally appeared on DEV Community and was authored by Kathan Vakharia


Print Share Comment Cite Upload Translate Updates
APA

Kathan Vakharia | Sciencx (2021-06-26T21:04:34+00:00) LinkedList Questions: Delete a given node in constant time. Retrieved from https://www.scien.cx/2021/06/26/linkedlist-questions-delete-a-given-node-in-constant-time/

MLA
" » LinkedList Questions: Delete a given node in constant time." Kathan Vakharia | Sciencx - Saturday June 26, 2021, https://www.scien.cx/2021/06/26/linkedlist-questions-delete-a-given-node-in-constant-time/
HARVARD
Kathan Vakharia | Sciencx Saturday June 26, 2021 » LinkedList Questions: Delete a given node in constant time., viewed ,<https://www.scien.cx/2021/06/26/linkedlist-questions-delete-a-given-node-in-constant-time/>
VANCOUVER
Kathan Vakharia | Sciencx - » LinkedList Questions: Delete a given node in constant time. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/26/linkedlist-questions-delete-a-given-node-in-constant-time/
CHICAGO
" » LinkedList Questions: Delete a given node in constant time." Kathan Vakharia | Sciencx - Accessed . https://www.scien.cx/2021/06/26/linkedlist-questions-delete-a-given-node-in-constant-time/
IEEE
" » LinkedList Questions: Delete a given node in constant time." Kathan Vakharia | Sciencx [Online]. Available: https://www.scien.cx/2021/06/26/linkedlist-questions-delete-a-given-node-in-constant-time/. [Accessed: ]
rf:citation
» LinkedList Questions: Delete a given node in constant time | Kathan Vakharia | Sciencx | https://www.scien.cx/2021/06/26/linkedlist-questions-delete-a-given-node-in-constant-time/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.