将一个链接列表分为两部分 [英] Splitting one linked list in two parts

查看:167
本文介绍了将一个链接列表分为两部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友!

我不知道如何将一个链接列表分为两部分.谁能帮我?

Hello friends!

I don''t know how to split one linked list in two parts. Can anyone help me?

// s is the original list
    s = {6,4,8,2,9}

// The resulting list would be l and m
    l = {6,4}
    m = {8,2,9}



我该如何实现呢?



How can I achieve this?

推荐答案

链表的头指针始终指向第一个节点.为第二个链接列表创建一个新的head指针(例如head2).现在遍历列表,并在列表中拆分该节点,并将指向该节点的指针分配给变量head2,并将上一个节点的下一个指针设置为null.

如果您向我们展示了用于实现链表的代码,将会更清楚地进行解释.
The head pointer of a linked list alway points the first node. Create a new head pointer (head2 for instance) for the second linked list. Now traverse through the list and where you want to split the list and assign the pointer to that node to variable head2 and set the next-pointer of the previous node to null.

It would be more clear to explain if you showed us the code you used to implement the linked list.


不可能分辨出分割标准是什么,因此它是无法产生将始终正常工作的算法.
It''s impossible to tell what the criterion for the split is and thusly it is impossible to produce an algorithm that will always work correctly.

  1. 一个可能的标准是遇到4时拆分.
  2. 另一种可能的准则是将列表拆分为两个相等的部分,并且当元素数为奇数时,第二个列表应更长
  3. 如果当前元素的大小是前一个元素的两倍,则列表应在前一个元素处拆分.
  4. 始终在第二个元素之后拆分列表.
  5. 如果有三个元素,第一个乘以最后一个等于中间元素,然后在第一个元素之后分割.
  6. 如果有三个连续的元素,其中最后一个减去第一个加一个等于中间元素然后在第一个元素之前拆分.
  7. ...

  1. One possible criterion would be to split when a 4 was encountered.
  2. Another one would be to split the list into two equal parts and when the number of elements is odd the seconde list shall be the longer part.
  3. The list shall be split at the previous element if the current element is twice as large as the previous one.
  4. Always split the list after the second element.
  5. If there are three elements where the first multiplied by the last equals the middle element then split after the first element.
  6. If there are three consecutive elements where the last minus the first plus one equals the middle element then split before the first element.
  7. ...



因此,请准确指定拆分的方式.

问候,

—MRB



So please specify exactly how the splitting is supposed to take place.

Regards,

—MRB


这篇关于将一个链接列表分为两部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆