-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
refactor: RotateListRight.js and added tests #1101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Ideally this should be a method of the SinglyLinkedList
class though; then you would also be able to leverage the get
method to obtain an array rather than having to do the tedious .head
. You could additionally add a constructor that takes a list to shorten your list creation.
@appgurueu Should I await your suggestion being implemented before merging?
@appgurueu Should I await your suggestion being implemented before merging?
Not sure. The PR is definitely already an improvement in its current state, but the ideal solution would be what I have suggested.
@10kartik Can you implement the suggested?
Yeah @raklaptudirm, I'll give it a try... But I'm a bit doubtful about the suggested implementation.
Ideally this should be a method of the
SinglyLinkedList
class though; then you would also be able to leverage theget
method to obtain an array rather than having to do the tedious.head
.
As per my understanding @appgurueu, adding a method RotateListRight
to LinkedList class will narrow it's usage.
Let's say in a list of length 10, I want to rotate the list after the 4th node, so I'd simply pass the reference of the 5th node as head
and k
steps. But if I'll add a method in LinkedList class that only accepts k
which rotates the list right from the first node, the method will simply lose the essence behind it.
Please correct me if I'm wrong or missing something here.
You could additionally add a constructor that takes a list to shorten your list creation.
For this, I must add a constructor to the LinkedList
class, pass an array/list
as an argument to it and call addLast to create a list, right?
As per my understanding @appgurueu, adding a method
RotateListRight
to LinkedList class will narrow it's usage.Let's say in a list of length 10, I want to rotate the list after the 4th node, so I'd simply pass the reference of the 5th node as
head
andk
steps. But if I'll add a method in LinkedList class that only acceptsk
which rotates the list right from the first node, the method will simply lose the essence behind it.Please correct me if I'm wrong or missing something here.
Valid point.
The clean solution to that would be allowing "views" into sublists, comparable to Python or Go slices (although those work on array-based lists). Views would obviously again be linked lists and could thus have rotateListRight
applied to them.
You could additionally add a constructor that takes a list to shorten your list creation.
For this, I must add a constructor to the
LinkedList
class, pass anarray/list
as an argument to it and call addLast to create a list, right?
Yes.
The clean solution to that would be allowing "views" into sublists, comparable to Python or Go slices (although those work on array-based lists). Views would obviously again be linked lists and could thus have
rotateListRight
applied to them.
Right! seems reasonable. Okay, so not sure how should I proceed with that 🤔. Should I shift the rotateRightList
function logic to LinkedList 's method, rotate list by k nodes and reset the head accordingly? Before that, I will add a constructor for creating LinkedList... Sounds good?
The clean solution to that would be allowing "views" into sublists, comparable to Python or Go slices (although those work on array-based lists). Views would obviously again be linked lists and could thus have
rotateListRight
applied to them.Right! seems reasonable. Okay, so not sure how should I proceed with that thinking. Should I shift the
rotateRightList
function logic to LinkedList 's method, rotate list by k nodes and reset the head accordingly? Before that, I will add a constructor for creating LinkedList... Sounds good?
Yes, sounds good. Methods to create views / sublists can come later (or not at all).
Open in Gitpod know more
Describe your change:
Checklist:
Example:
UserProfile.js
is allowed butuserprofile.js
,Userprofile.js
,user-Profile.js
,userProfile.js
are notFixes: #{$ISSUE_NO}
.