Chitresh-code / DSA_Worksheet

Worksheet posting DSA problems every day for GNIOT Students to solve and create PR's.
MIT License
17 stars 33 forks source link

Remove Nth node from end of List #7

Closed Faraaz22 closed 1 year ago

Faraaz22 commented 1 year ago

Kindly assign this to me under hacktober fest.

Problem Given the head of a linked list, remove the nth node from the end of the list and return its head.

Example 1:

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

Example 2:

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

Example 3:

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

Constraints:

The number of nodes in the list is sz.
 1 <= sz <= 30
 0 <= Node.val <= 100
 1 <= n <= sz
yanurag1414 commented 1 year ago

Assign this work to me.

Chitresh-code commented 1 year ago

Done @yanurag1414 Happy Coding!

gitaditee commented 1 year ago

@Faraaz22 can you please assign this task to me

y9rabbito commented 1 year ago

Try to solve it in optimise way in real world scenarion it will take linear time O(n) to execute the code. Try to check if its the last node or the only node then delete and return null; Otherwise just swap Nth Node and N+1 Node values then delete N+1 node values Complexity - O(1) Constant time in apriori analysis.

gitaditee commented 1 year ago

Ok sure @y9rabbito

gitaditee commented 1 year ago

@y9rabbito can you check the solution and let me know if any changes are required

lilmistake commented 1 year ago

Issue solved with https://github.com/Chitresh-code/DSA_Worksheet/pull/8 https://github.com/Chitresh-code/DSA_Worksheet/pull/13 https://github.com/Chitresh-code/DSA_Worksheet/pull/14 and https://github.com/Chitresh-code/DSA_Worksheet/pull/15