Last active
November 3, 2019 01:25
-
-
Save Nimblz/e8516647b2a75080cd09bbad5cafde8f to your computer and use it in GitHub Desktop.
LinkedList rotation solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Running test: example1 | |
Input: [7 -> 7 -> 3 -> 5] | |
Rotation: 2 | |
Expected: [3 -> 5 -> 7 -> 7] | |
Got: [3 -> 5 -> 7 -> 7] | |
Success! | |
Running test: example2 | |
Input: [1 -> 2 -> 3 -> 4 -> 5] | |
Rotation: 3 | |
Expected: [3 -> 4 -> 5 -> 1 -> 2] | |
Got: [3 -> 4 -> 5 -> 1 -> 2] | |
Success! | |
Running test: numbers | |
Input: [1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 0] | |
Rotation: 1 | |
Expected: [0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9] | |
Got: [0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9] | |
Success! | |
Running test: noRotation | |
Input: [1 -> 2 -> 3 -> 4 -> 5] | |
Rotation: 0 | |
Expected: [1 -> 2 -> 3 -> 4 -> 5] | |
Got: [1 -> 2 -> 3 -> 4 -> 5] | |
Success! | |
Running test: fullRotation | |
Input: [1 -> 2 -> 3 -> 4 -> 5] | |
Rotation: 5 | |
Expected: [1 -> 2 -> 3 -> 4 -> 5] | |
Got: [1 -> 2 -> 3 -> 4 -> 5] | |
Success! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function link(value,next) | |
return { | |
value = value, | |
next = next, | |
} | |
end | |
local function linkedList(...) | |
local numArgs = select("#", ...) | |
local lastLink | |
local headLink | |
for i = numArgs, 1, -1 do | |
local value = select(i, ...) | |
local newLink = link(value, lastLink) | |
lastLink = newLink | |
if i == numArgs then | |
headLink = newLink | |
end | |
end | |
return { | |
root = lastLink, | |
head = headLink, | |
} | |
end | |
local function traverseList(list, callback) | |
local node = list.root | |
if node == nil then return end | |
repeat | |
callback(node) | |
node = node.next | |
until node == nil | |
end | |
local function listToString(list) | |
local values = {} | |
traverseList(list, function(node) | |
table.insert(values, node.value) | |
end) | |
return "["..table.concat(values, " -> ").."]" | |
end | |
local function listLength(list) | |
local counter = 0 | |
traverseList(list, function(node) | |
counter = counter + 1 | |
end) | |
return counter | |
end | |
local function copyList(list) | |
local valuesToCopy = {} | |
traverseList(list, function(node) | |
local value = node.value | |
table.insert(valuesToCopy, value) | |
end) | |
return linkedList(unpack(valuesToCopy)) | |
end | |
local function joinLists(list1, list2) | |
local copy1 = copyList(list1) | |
local copy2 = copyList(list2) | |
if copy1.head == nil then return copy2 end | |
copy1.head.next = copy2.root | |
return copy1 | |
end | |
local function splitList(list, k) | |
local counter = 1 | |
local list1Values = {} | |
local list2Values = {} | |
traverseList(list, function(node) | |
if counter <= k then | |
table.insert(list1Values, node.value) | |
else | |
table.insert(list2Values, node.value) | |
end | |
counter = counter + 1 | |
end) | |
local list1 = linkedList(unpack(list1Values)) | |
local list2 = linkedList(unpack(list2Values)) | |
return list1, list2 | |
end | |
local function areLinkedListsEqual(list1, list2) | |
local node1 = list1.root | |
local node2 = list2.root | |
repeat | |
if node1.value ~= node2.value then | |
return false | |
end | |
node1 = node1.next | |
node2 = node2.next | |
until node1 == nil and node2 == nil | |
return true | |
end | |
local function rotateLinkedList(list, k) | |
-- split the list at k traversals from head | |
-- create a new list with the split lists in oposite order | |
local length = listLength(list) | |
local list1, list2 = splitList(list,length-k) | |
local joined = joinLists(list2,list1) | |
return joined | |
end | |
local tests = { | |
{ | |
name = "example1", | |
list = linkedList(7,7,3,5), | |
k = 2, | |
expected = linkedList(3,5,7,7), | |
}, | |
{ | |
name = "example2", | |
list = linkedList(1,2,3,4,5), | |
k = 3, | |
expected = linkedList(3,4,5,1,2), | |
}, | |
{ | |
name = "numbers", | |
list = linkedList(1,2,3,4,5,6,7,8,9,0), | |
k = 1, | |
expected = linkedList(0,1,2,3,4,5,6,7,8,9), | |
}, | |
{ | |
name = "noRotation", | |
list = linkedList(1,2,3,4,5), | |
k = 0, | |
expected = linkedList(1,2,3,4,5), | |
}, | |
{ | |
name = "fullRotation", | |
list = linkedList(1,2,3,4,5), | |
k = 5, | |
expected = linkedList(1,2,3,4,5), | |
}, | |
} | |
for index, test in ipairs(tests) do | |
print(("Running test: %s"):format(test.name)) | |
local outlist = rotateLinkedList(test.list, test.k) | |
print("\tInput:\t\t", listToString(test.list)) | |
print("\tRotation:\t", test.k) | |
print("\tExpected:\t", listToString(test.expected)) | |
print("\tGot:\t\t", listToString(outlist)) | |
print("\n\t"..(areLinkedListsEqual(outlist, test.expected) and "Success!" or "FAILED!").."\n") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment