I need to break a singly linked list into smaller linked lists after every 2 nodes . The approach I thought was,
- create an array containign head point开发者_如何转开发ers of n/2 objects
- Link hop the linked list and store the address in the array after every 2 nodes are encountered. Can there be a better approach for this?
Thanks.
That seems like a good approach.
You also need to remember to set the next
member of the 2nd, 4th, etc... elements to null to break the long list into smaller pieces. Remember to store the old value before you overwrite it as you will need to use it while you iterate.
精彩评论