6 kyu

Linked Lists - Sorted Insert

1,082 of 2,403JDeBolt

Description:

Linked Lists - Sorted Insert

Write a SortedInsert() function which inserts a node into the correct location of a pre-sorted linked list which is sorted in ascending order. SortedInsert takes the head of a linked list and data used to create a node as arguments. SortedInsert() should also return the head of the list.

sortedInsert(1 -> 2 -> 3 -> null, 4) === 1 -> 2 -> 3 -> 4 -> null)
sortedInsert(1 -> 7 -> 8 -> null, 5) === 1 -> 5 -> 7 -> 8 -> null)
sortedInsert(3 -> 5 -> 9 -> null, 7) === 3 -> 5 -> 7 -> 9 -> null)
sortedInsert(1 -> 2 -> 3 -> null, 4) === 1 -> 2 -> 3 -> 4 -> null)
sortedInsert(1 -> 7 -> 8 -> null, 5) === 1 -> 5 -> 7 -> 8 -> null)
sortedInsert(3 -> 5 -> 9 -> null, 7) === 3 -> 5 -> 7 -> 9 -> null)

The push() and buildOneTwoThree() functions do not need to be redefined.

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 13, 2015
PublishedAug 14, 2015
Warriors Trained4360
Total Skips584
Total Code Submissions7752
Total Times Completed2403
JavaScript Completions1082
Python Completions1143
Swift Completions190
Ruby Completions46
CoffeeScript Completions11
Total Stars87
% of votes with a positive feedback rating94% of 382
Total "Very Satisfied" Votes339
Total "Somewhat Satisfied" Votes38
Total "Not Satisfied" Votes5
Ad
Contributors
  • JDeBolt Avatar
  • CIS 122 Avatar
  • CorsaiR Avatar
  • alexandersmanning Avatar
  • Blind4Basics Avatar
  • user9644768 Avatar
  • cliffstamp Avatar
Ad