Linked Lists - Front Back Split
Description:
Linked Lists - Front Back Split
Write a FrontBackSplit() function that takes one list and splits it into two sublists — one for the front half, and one for the back half. If the number of elements is odd, the extra element should go in the front list. For example, FrontBackSplit() on the list 2 -> 3 -> 5 -> 7 -> 11 -> null
should yield the two lists 2 -> 3 -> 5 -> null
and 7 -> 11 -> null
. Getting this right for all the cases is harder than it looks. You will probably need special case code to deal with lists of length < 2 cases.
var source = 1 -> 3 -> 7 -> 8 -> 11 -> 12 -> 14 -> null
var front = new Node()
var back = new Node()
frontBackSplit(source, front, back)
front === 1 -> 3 -> 7 -> 8 -> null
back === 11 -> 12 -> 14 -> null
You should throw an error if any of the arguments to FrontBackSplit are null or if the source list has < 2 nodes.
Hint. Probably the simplest strategy is to compute the length of the list, then use a for loop to hop over the right number of nodes to find the last node of the front half, and then cut the list at that point. There is a trick technique that uses two pointers to traverse the list. A "slow" pointer advances one nodes at a time, while the "fast" pointer goes two nodes at a time. When the fast pointer reaches the end, the slow pointer will be about half way. For either strategy, care is required to split the list at the right point.
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.
Similar Kata:
Stats:
Created | Aug 29, 2015 |
Published | Aug 31, 2015 |
Warriors Trained | 3949 |
Total Skips | 1352 |
Total Code Submissions | 5214 |
Total Times Completed | 879 |
JavaScript Completions | 350 |
Python Completions | 400 |
Swift Completions | 153 |
Total Stars | 92 |
% of votes with a positive feedback rating | 88% of 184 |
Total "Very Satisfied" Votes | 153 |
Total "Somewhat Satisfied" Votes | 17 |
Total "Not Satisfied" Votes | 14 |