简体   繁体   English

如何仅通过重新排列链接来重新排列 Java 中的 LinkedList 而不更改数据字段?

[英]How can I rearrange a LinkedList in Java without changing the data fields, only by rearranging the links?

So first of all, I just started learning about LinkedLists, so I'm sorry if I say something about it and it's totally wrong.所以首先,我刚开始学习 LinkedLists,所以如果我对它说些什么,那我很抱歉,这是完全错误的。 I have an assignment where I have to write a method to switch the pairs of a LinkedList.我有一个任务,我必须编写一个方法来切换成对的 LinkedList。 If I have a list [3, 7, 4, 9], it would change to [7, 3, 9, 4] after the method.如果我有一个列表 [3, 7, 4, 9],它会在方法之后更改为 [7, 3, 9, 4]。 I'm not allowed to change any of the data fields of the nodes nor can I construct any new nodes.我不能更改节点的任何数据字段,也不能构造任何新节点。 I'm trying to understand how LinkedLists work.我试图了解 LinkedLists 是如何工作的。 So far this is what I have:到目前为止,这就是我所拥有的:

public void switchPairs() {
    if (front == null)
        return;
    ListNode current = head;
    ListNode next = head.next;
    current.next = null;

    next.next = current;

}

Right now I'm still trying to figure out how I can swap the first two values.现在我仍在试图弄清楚如何交换前两个值。 My reasoning is that I can assign a new ListNode variable called current to the head, and then another new ListNode variable called next to head.next.我的理由是,我可以将一个名为 current 的新 ListNode 变量分配给 head,然后将另一个名为 next 的新 ListNode 变量分配给 head.next。 Then I can assign the node after current to be null.然后我可以将当前之后的节点分配为null。 Then that list is separate from the next variable that I created and I can assign the node after next to be current.然后该列表与我创建的下一个变量分开,我可以将下一个节点分配为当前节点。 But that doesn't work.但这不起作用。 So how can I go about doing this?那么我该怎么做呢?

You need to follow as below.您需要按照以下说明进行操作。

1. While temp is not null && temp1 is not null
      1. Swap links of temp and temp1
      2. Move temp to temp1.next
      3. Move temp1 to temp.next if temp is not null 
                                 else make temp is null

swap two node links is as below.交换两个节点链接如下。 [switchPairs(temp, temp1)] [switchPairs(temp, temp1)]

 1. newTemp = temp
 2. temp.link = temp1.link
 3. temp1.link = newTemp

Let's say比方说

data|link is the pattern I am using here. data|link 是我在这里使用的模式。

temp = 3|linkX 

temp1 = 5|linkY

newTemp = temp = Points to the address of Temp

temp.link = 3|linkY [temp.link = temp1.link]

temp1.link = 5|AdrOfTemp [temp1.link = newTemp]

If you still have doubts, please post your helper method which calls this function.如果您仍有疑问,请发布调用此 function 的辅助方法。 I doubt that you may missed to switch the temp nodes.我怀疑您可能会错过切换临时节点。

Please refer the code below:请参考以下代码:

public void switchPairs() {
if (front == null)
    return;

ListNode current = head;
ListNode nextNode = head.next;
current.next = nextNode.next;
head = nextNode; 
head.next = current;

} }

Hope this algorithm will help...希望这个算法能帮助...

  1. Assign the head node into the one temp node将头节点分配到一个临时节点
  2. Assign the head's next node into the another temp node将头的下一个节点分配到另一个临时节点
  3. Replace the step 1's next node with the step 2's next node将步骤 1 的下一个节点替换为步骤 2 的下一个节点
  4. Assign the head next node with the node created on step 1使用在步骤 1 中创建的节点分配头下一个节点
  5. Assign the head node with the node created on step 2将头节点分配给在步骤 2 中创建的节点

Since this is your school assignment, I am not giving you the complete solution.由于这是你的学校作业,我不会给你完整的解决方案。 Rather, I'm writing solution to only the following requirement and I hope, you'll be able to complete the rest of your assignment with the help of this code.相反,我只针对以下要求编写解决方案,我希望您能够在此代码的帮助下完成您的作业的 rest。

Right now I'm still trying to figure out how I can swap the first two values.现在我仍在试图弄清楚如何交换前两个值。

public ListNode switchPairs(ListNode head) {
    if (head == null)
        return;
    ListNode current = head;
    head = current.getNext();
    current.setNext(head.getNext());
    head.setNext(current);
    return head;
}

Notes:笔记:

  1. I have changed the return type from void to ListNode .我已将返回类型从void更改为ListNode You'll need to call it like你需要这样称呼它
head = switchPairs(head);

from wherever (eg main ) you have created the linked list.从您创建链接列表的任何位置(例如main )。 Wherever you have created the linked list, you must have first created the head node and then added other nodes to it.无论您在何处创建了链表,都必须先创建head节点,然后再向其中添加其他节点。 After calling switchPairs , the head should point to the second node in the original linked list.调用switchPairs后, head应该指向原始链表中的第二个节点。

  1. You should declare next as private and write public getter and setter for it as shown below:您应该将next声明为private并为其编写public getter 和 setter,如下所示:
public ListNode getNext() {
    return next;
} 
setNext(ListNode next) {
    this.next = next;
}

Note that your IDE can generate these two methods on a couple of clicks.请注意,您的 IDE 只需单击几下即可生成这两种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM