5 kyu
Swap Node Pairs In Linked List
349 of 834natehart
Loading description...
Algorithms
Data Structures
Linked Lists
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
Java Fork enforcing that the swapping is done in-place, to align with the other languages. the author's original solution still passes the tests, so correct solutions should be preserved.
Python translation
Approved
Rust translation
Is there any attempt to translate this kata into Python yet? Would allow more people to play. :)
it's there ;-)
The kata requires actually to return a linked list with swapped values, it doesn't check that the nodes from the input list have actually been swapped. It is not about
writ(ing) a method that swaps each pair of nodes in the list
(though it works too). The description could make this clearer.Description says:
If tests accept lists with swapped values instead of swapped nodes, it's an issue. Which language?
At least in Java (my solution returns a brand new list only based on values). I don't know for JS, C requires in place mutation.
Javascript looks ok.
Add tag
Linked lists
added
Had a good time solving this kata, thanks
C translation
Approved
Behaviour on lists with an odd number of nodes is unspecified.
fixed
Javascript translation
approved (use suggestion tag next time)
Java translation adding random tests and fixing the issue from Voile (link)
There are no random tests.
Added :D
Thank You! Very nice kata, looking forward to more of such DS katas.
you do not provide an important information: can we mutate the linked list or do we have to provide a fresh one?
I think the better solution is to do it in place, but it is not important. The only thing that is not allowed (per the way the Node is coded) you cannot swap values around.
I hesitated with an issue, but well... Let's do this with a suggestion:
You should implement the
toString
method for the classNode
, so taht the assertion message will be meaningful.Good call. I've also added an easy way to print the list so people can debug it more easily.
Nice kata, thanks. :-) As Java doesn't have tail recursion optimization and you really wouldn't want a recursive solution to make it into production it would be good to either add tests with enough nodes to make recursive solutions crash with a StackOverflowError or define some rather restrictive constraints for the input, e.g. a maximum of 500 nodes in the passed linked list.
Had a lot of fun with this one. Thanks! :-)