6 kyu

Linked Lists - Insert Nth Node

1,179 of 2,830JDeBolt

Description:

Linked Lists - Insert Nth

Implement an InsertNth() function (insert_nth() in PHP) which can insert a new node at any index within a list.

InsertNth() is a more general version of the Push() function that we implemented in the first kata listed below. Given a list, an index 'n' in the range 0..length, and a data element, add a new node to the list so that it has the given index. InsertNth() should return the head of the list.

insertNth(1 -> 2 -> 3 -> null, 0, 7) === 7 -> 1 -> 2 -> 3 -> null)
insertNth(1 -> 2 -> 3 -> null, 1, 7) === 1 -> 7 -> 2 -> 3 -> null)
insertNth(1 -> 2 -> 3 -> null, 3, 7) === 1 -> 2 -> 3 -> 7 -> null)
insert_nth(1 -> 2 -> 3 -> NULL, 0, 7); // 7 -> 1 -> 2 -> 3 -> NULL
insert_nth(1 -> 2 -> 3 -> NULL, 1, 7); // 1 -> 7 -> 2 -> 3 -> NULL
insert_nth(1 -> 2 -> 3 -> NULL, 3, 7); // 1 -> 2 -> 3 -> 7 -> NULL
insert_nth(1 -> 2 -> 3 -> NULL, 4, 7); // throws new InvalidArgumentException
Node.InsertNth(1 -> 2 -> 3 -> null, 0, 7)  => 7 -> 1 -> 2 -> 3 -> null
Node.InsertNth(1 -> 2 -> 3 -> null, 1, 7)  => 1 -> 7 -> 2 -> 3 -> null
Node.InsertNth(1 -> 2 -> 3 -> null, 3, 7)  => 1 -> 2 -> 3 -> 7 -> null
Node.InsertNth(1 -> 2 -> 3 -> null, -2, 7) // throws new ArgumentException

You must throw/raise an exception (ArgumentOutOfRangeException in C#, InvalidArgumentException in PHP) if the index is too large.

The push() and buildOneTwoThree() (build_one_two_three() in PHP) functions do not need to be redefined. The Node class is also preloaded for you in PHP.

Related Kata in order of expected completion (increasing difficulty):
 Linked Lists - Push & BuildOneTwoThree
 Linked Lists - Length & Count
 Linked Lists - Get Nth Node
Linked Lists - Insert Nth Node
Linked Lists - Sorted Insert
Linked Lists - Insert Sort
Linked Lists - Append
Linked Lists - Remove Duplicates
Linked Lists - Move Node
Linked Lists - Move Node In-place
Linked Lists - Alternating Split
Linked Lists - Front Back Split
Linked Lists - Shuffle Merge
Linked Lists - Sorted Merge
Linked Lists - Merge Sort
Linked Lists - Sorted Intersect
Linked Lists - Iterative Reverse
Linked Lists - Recursive Reverse

Inspired by Stanford Professor Nick Parlante's excellent Linked List teachings.

Linked Lists
Data Structures
Fundamentals

Stats:

CreatedAug 12, 2015
PublishedAug 12, 2015
Warriors Trained5865
Total Skips1050
Total Code Submissions10816
Total Times Completed2830
JavaScript Completions1179
Python Completions1200
Ruby Completions127
Swift Completions197
PHP Completions51
C# Completions157
CoffeeScript Completions6
Total Stars107
% of votes with a positive feedback rating93% of 459
Total "Very Satisfied" Votes404
Total "Somewhat Satisfied" Votes45
Total "Not Satisfied" Votes10
Ad
Contributors
  • JDeBolt Avatar
  • CIS 122 Avatar
  • lysannschlegel Avatar
  • donaldsebleung Avatar
  • Voile Avatar
  • Souzooka Avatar
  • Bubbler Avatar
  • user9644768 Avatar
  • hobovsky Avatar
  • dfhwze Avatar
  • LosBlobbos Avatar
Ad