Remove nth Node from End of List

Try to solve the Remove nth Node From End of List problem.

Statement#

Given a singly linked list, remove the nthn^{th} node from the end of the list and return its head.

Constraints:

  • The number of nodes in the list is k.
  • 11 \leq k 103\leq 10^3
  • 103-10^3 \le Node.value 103\le 10^3
  • 11 \le n \le k

Examples#

Created with Fabric.js 3.6.6 Sample example 1 Output Input ---------------------------------------------------------------------- n = 1 NULL 43 68 11 5 69 37 70 NULL 43 68 11 5 69 37

1 of 4

Created with Fabric.js 3.6.6 Output NULL 23 28 10 5 39 70 Input n = 3 ---------------------------------------------------------------------- Sample example 2 67 39 NULL 23 28 10 5 70

2 of 4

Created with Fabric.js 3.6.6 Output 3 4 NULL 1 2 5 6 7 NULL 1 2 4 5 6 7 Input n = 5 ---------------------------------------------------------------------- Sample example 3

3 of 4

Created with Fabric.js 3.6.6 Output 50 40 NULL 30 20 NULL 40 30 20 Input n = 4 ---------------------------------------------------------------------- Sample example 4

4 of 4

Understand the problem#

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Remove Nth Node From End of List

1

What is the output if the following linked list and value of nn are given as input?

Linked list: 32 → 78 → 65 → 90 → 12 → 44 → NULL

nn = 3

A)

32 → 78 → 90 → 12 → 44 → NULL

B)

32 → 78 → 65 → 12 → 44 → NULL

C)

78 → 65 → 90 → 12 → 44 → NULL

D)

32 → 78 → 65 → 90 → 12 → NULL

Question 1 of 30 attempted

Figure it out!#

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Drag and drop the cards to rearrange them in the correct sequence.

Set two pointers, right and left, at the head of the linked list.

Move the right pointer n steps forward.

Move both the right and left pointers forward until the right pointer reaches the last node. At this point, the left pointer will be pointing to the node behind the nth last node.

Relink the left node to the node next to left’s next node.

Return the head of the linked list.


Try it yourself#

Implement your solution in main.py in the following coding playground. You will need the provided supporting code to implement your solution.

Python
main.py
linked_list_node.py
linked_list.py
Input #1
Input #2
%0 node_01 23 node_11 28 node_01->node_11 node_21 10 node_11->node_21 node_31 5 node_21->node_31 node_41 67 node_31->node_41 node_51 39 node_41->node_51 node_61 70 node_51->node_61 node_71 28 node_61->node_71
Visualization for Input #1
Remove Nth Node From End of List

Solution: Valid Palindrome

Solution: Remove nth Node from End of List