开发者

Break the linked list into smaller linked lists

开发者 https://www.devze.com 2023-04-09 13:09 出处:网络
I need to break a singly linked list into smaller linked lists after every 2 nodes . The approach I thought was,

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号