Linked Lists: Given a list, split it into two sublists — one for the fronthalf,
ID: 3614469 • Letter: L
Question
Linked Lists:Given a list, split it into two sublists — one for the fronthalf, and one for the back half. If the number of elements is odd,the extra element should go in the front list. So FrontBackSplit()on the list {2, 3, 5, 7, 11} should yield the two lists {2, 3, 5}and {7, 11}. Getting this right for all the cases is harder than itlooks. You should check your solution against a few cases (length =2, length = 3, length=4) to make sure that the list gets splitcorrectly near the short-list boundary conditions. If it worksright for length=4, it probably works right for length=1000. Youwill probably need special case code to deal with the (length<2) cases.
Hint: Probably the simplest strategy is to compute the length ofthe list, then use a for loop to hop over the right number of nodesto find the last node of the front halt, and then cut the list atthat point. there is a trick technique that uses two pointers totraverse the list. A "slow" pointer advances one nodes at a time,while the "fast" pointer goes two nodes at a time. when the fastpointer reaches the end, the slow pointer will be about half way.for either strategy, care is required to split the list at theright point. Linked Lists:
Given a list, split it into two sublists — one for the fronthalf, and one for the back half. If the number of elements is odd,the extra element should go in the front list. So FrontBackSplit()on the list {2, 3, 5, 7, 11} should yield the two lists {2, 3, 5}and {7, 11}. Getting this right for all the cases is harder than itlooks. You should check your solution against a few cases (length =2, length = 3, length=4) to make sure that the list gets splitcorrectly near the short-list boundary conditions. If it worksright for length=4, it probably works right for length=1000. Youwill probably need special case code to deal with the (length<2) cases.
Hint: Probably the simplest strategy is to compute the length ofthe list, then use a for loop to hop over the right number of nodesto find the last node of the front halt, and then cut the list atthat point. there is a trick technique that uses two pointers totraverse the list. A "slow" pointer advances one nodes at a time,while the "fast" pointer goes two nodes at a time. when the fastpointer reaches the end, the slow pointer will be about half way.for either strategy, care is required to split the list at theright point.
Explanation / Answer
!!??
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.