`{"id":"efc3de88-e5b0-413a-8f87-3231212be1f0","name":"Remove Nth Node From End Of Linkedlist","description":"Given a singly linklist, remove the nth node from the end of the list and return its head.\r\n","inputFormat":"1->7->2->6->3->5->4->null\r\n2","outputFormat":"1->7->2->6->3->4->null","constraints":"1 &lt;= size Of LinkedList &lt;= 10^6\r\n1 &lt;= n &lt;= 10^6\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\nusing namespace std;\r\n\r\nclass ListNode\r\n{\r\npublic:\r\n int val = 0;\r\n ListNode* next = nullptr;\r\n\r\n ListNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nListNode* removeNthFromEnd(ListNode* head, int n)\r\n{\r\n return nullptr;\r\n}\r\n\r\nvoid printList(ListNode* node)\r\n{\r\n ListNode* curr = node;\r\n while (curr != nullptr)\r\n {\r\n cout << curr->val << \" \";\r\n curr = curr->next;\r\n }\r\n cout << endl;\r\n}\r\n\r\nListNode* createList(int n)\r\n{\r\n ListNode* dummy = new ListNode(-1);\r\n ListNode* prev = dummy;\r\n while (n-- > 0)\r\n {\r\n int val;\r\n cin >> val;\r\n prev->next = new ListNode(val);\r\n prev = prev->next;\r\n }\r\n return dummy->next\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n ListNode* h1 = createList(n);\r\n int m;\r\n cin >> m;\r\n h1 = removeNthFromEnd(h1, m);\r\n printList(h1);\r\n\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class ListNode {\r\n int val = 0;\r\n ListNode next = null;\r\n\r\n ListNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ListNode removeNthFromEnd(ListNode head, int n) {\r\n return null;\r\n }\r\n\r\n public static void printList(ListNode node) {\r\n while (node != null) {\r\n System.out.print(node.val + \" \");\r\n node = node.next;\r\n }\r\n }\r\n\r\n public static ListNode createList(int n) {\r\n ListNode dummy = new ListNode(-1);\r\n ListNode prev = dummy;\r\n while (n-- > 0) {\r\n prev.next = new ListNode(scn.nextInt());\r\n prev = prev.next;\r\n }\r\n\r\n return dummy.next;\r\n }\r\n\r\n public static void main(String[] args) {\r\n int n = scn.nextInt();\r\n ListNode h1 = createList(n);\r\n\r\n int m = scn.nextInt();\r\n h1 = removeNthFromEnd(h1, m);\r\n printList(h1);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"9\r\n18 1 16 11 15 7 9 16 4 \r\n1\r\n","sampleOutput":"18 1 16 11 15 7 9 16 ","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"b430e749-30c3-4594-80cf-0721168a44f9","name":"Remove Nth Node From End Of Linkedlist","slug":"remove-nth-node-from-end-of-linkedlist","type":1}],"next":{"id":"f94186b3-39a3-45b6-968c-ceff749122b8","name":"Remove Nth Node From End Of Linkedlist Easy MCQ","type":0,"slug":"remove-nth-node-from-end-of-linkedlist-easy-mcq"},"prev":{"id":"d1f13985-5ebb-4f02-8685-cc8cae8af964","name":"Middle of a linked list","type":3,"slug":"middle-of-a-linked-list"}}}`

# Remove Nth Node From End Of Linkedlist

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

`{"id":"efc3de88-e5b0-413a-8f87-3231212be1f0","name":"Remove Nth Node From End Of Linkedlist","description":"Given a singly linklist, remove the nth node from the end of the list and return its head.\r\n","inputFormat":"1->7->2->6->3->5->4->null\r\n2","outputFormat":"1->7->2->6->3->4->null","constraints":"1 &lt;= size Of LinkedList &lt;= 10^6\r\n1 &lt;= n &lt;= 10^6\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\nusing namespace std;\r\n\r\nclass ListNode\r\n{\r\npublic:\r\n int val = 0;\r\n ListNode* next = nullptr;\r\n\r\n ListNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nListNode* removeNthFromEnd(ListNode* head, int n)\r\n{\r\n return nullptr;\r\n}\r\n\r\nvoid printList(ListNode* node)\r\n{\r\n ListNode* curr = node;\r\n while (curr != nullptr)\r\n {\r\n cout << curr->val << \" \";\r\n curr = curr->next;\r\n }\r\n cout << endl;\r\n}\r\n\r\nListNode* createList(int n)\r\n{\r\n ListNode* dummy = new ListNode(-1);\r\n ListNode* prev = dummy;\r\n while (n-- > 0)\r\n {\r\n int val;\r\n cin >> val;\r\n prev->next = new ListNode(val);\r\n prev = prev->next;\r\n }\r\n return dummy->next\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n ListNode* h1 = createList(n);\r\n int m;\r\n cin >> m;\r\n h1 = removeNthFromEnd(h1, m);\r\n printList(h1);\r\n\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class ListNode {\r\n int val = 0;\r\n ListNode next = null;\r\n\r\n ListNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ListNode removeNthFromEnd(ListNode head, int n) {\r\n return null;\r\n }\r\n\r\n public static void printList(ListNode node) {\r\n while (node != null) {\r\n System.out.print(node.val + \" \");\r\n node = node.next;\r\n }\r\n }\r\n\r\n public static ListNode createList(int n) {\r\n ListNode dummy = new ListNode(-1);\r\n ListNode prev = dummy;\r\n while (n-- > 0) {\r\n prev.next = new ListNode(scn.nextInt());\r\n prev = prev.next;\r\n }\r\n\r\n return dummy.next;\r\n }\r\n\r\n public static void main(String[] args) {\r\n int n = scn.nextInt();\r\n ListNode h1 = createList(n);\r\n\r\n int m = scn.nextInt();\r\n h1 = removeNthFromEnd(h1, m);\r\n printList(h1);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"9\r\n18 1 16 11 15 7 9 16 4 \r\n1\r\n","sampleOutput":"18 1 16 11 15 7 9 16 ","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"b430e749-30c3-4594-80cf-0721168a44f9","name":"Remove Nth Node From End Of Linkedlist","slug":"remove-nth-node-from-end-of-linkedlist","type":1}],"next":{"id":"f94186b3-39a3-45b6-968c-ceff749122b8","name":"Remove Nth Node From End Of Linkedlist Easy MCQ","type":0,"slug":"remove-nth-node-from-end-of-linkedlist-easy-mcq"},"prev":{"id":"d1f13985-5ebb-4f02-8685-cc8cae8af964","name":"Middle of a linked list","type":3,"slug":"middle-of-a-linked-list"}}}`

Editor

# Remove Nth Node From End Of Linkedlist

easy

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

## Constraints

1 <= size Of LinkedList <= 10^6 1 <= n <= 10^6

## Format

### Input

1->7->2->6->3->5->4->null 2

### Output

1->7->2->6->3->4->null

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}9 18 1 16 11 15 7 9 16 4 1 ```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}18 1 16 11 15 7 9 16 `

Discussions

Show Discussion

Related Resources