Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Java实现链表中的交换对元素_Java_Linked List_Nodes_Swap - Fatal编程技术网

用Java实现链表中的交换对元素

用Java实现链表中的交换对元素,java,linked-list,nodes,swap,Java,Linked List,Nodes,Swap,我已经在Java中为linkedList的swap元素编写了一个代码。 目前,我的代码失败,即它不是交换元素。我有困难 关于如何处理这个问题。有什么建议吗 public void switchPairs(){ if (front==null || front.next==null) return ; ListNode temp=front; front=front.next; ListNode curr=front; while(curr.

我已经在
Java
中为
linkedList
swap
元素编写了一个代码。 目前,我的代码失败,即它不是
交换
元素。我有困难 关于如何处理这个问题。有什么建议吗

public void switchPairs(){
    if (front==null || front.next==null)
        return ;
    ListNode temp=front;
    front=front.next;
    ListNode curr=front;
    while(curr.next!=null){
        ListNode dummy = curr.next;
        curr.next=temp;
        temp.next=dummy;
        temp=temp.next;
        curr=dummy;
    }
}

Input          : front -> [3] -> [7] -> [4] -> [9] -> [8] -> [12] / 
Expected output: front -> [7] -> [3] -> [9] -> [4] -> [12] -> [8] /     
my output:       front -> [7] -> [3] -> [4] -> [9] -> [8] -> [12] /

回答这样一个问题的最好方法是在迭代过程中可视化列表的状态。我已经用println实现了代码来帮助实现它。另一种选择是包含更容易跟踪的变量名,而
temp
dummy
不会阻止您实现正确性,因为它们更难遵循

这就是函数

    public ListNode switchPairs(){
        if (this==null || this.next==null)
            return this;
        ListNode top = this.next;

        ListNode first = this;
        ListNode second = first.next;

        do {
            ListNode third = second.next;
            second.next = first;
            first.next = third;
            first = third;
            System.out.println("@@@ " + top.toString());
            if (first != null) {
                // remember second now is really the first element on the list
                // at this point.
                second.next.next = first.next;
                second = first.next;
            }

        } while(first != null && second != null);

        return top;
    }
这就是全部代码

public class ListNode {
    private ListNode next = null;
    private final int i;
    ListNode(int i) {
        this.i = i;
    }

    ListNode(int i, ListNode parent) {
        this(i);
        parent.next = this;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("[" + this.i + "]");
        if (this.next != null) {
            sb.append("->");
            sb.append(this.next.toString());
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        ListNode top = null;
        ListNode curr = null;
        for(String arg : args) {
            int i = Integer.parseInt(arg);
            if(curr == null) 
                curr = new ListNode(i);
            else
                curr = new ListNode(i, curr);
            if( top == null) 
                top = curr;
        }
        System.out.println(top.toString());
        top = top.switchPairs();
        System.out.println(top.toString());
    }

    public ListNode switchPairs(){
        if (this==null || this.next==null)
            return this;
        ListNode top = this.next;

        ListNode first = this;
        ListNode second = first.next;

        do {
            ListNode third = second.next;
            second.next = first;
            first.next = third;
            first = third;
            System.out.println("@@@ " + this.toString());
            if (first != null) {
                second.next.next = first.next;
                second = first.next;
            }

        } while(first != null && second != null);

        return top;
    }
}
最后但并非最不重要的一个示例输出

java ListNode 1 2 3 4 5 6 7 8
[1]->[2]->[3]->[4]->[5]->[6]->[7]->[8]
@@@ [2]->[1]->[3]->[4]->[5]->[6]->[7]->[8]
@@@ [2]->[1]->[4]->[3]->[5]->[6]->[7]->[8]
@@@ [2]->[1]->[4]->[3]->[6]->[5]->[7]->[8]
@@@ [2]->[1]->[4]->[3]->[6]->[5]->[8]->[7]
[2]->[1]->[4]->[3]->[6]->[5]->[8]->[7]

回答这样一个问题的最好方法是在迭代过程中可视化列表的状态。我已经用println实现了代码来帮助实现它。另一种选择是包含更容易跟踪的变量名,而
temp
dummy
不会阻止您实现正确性,因为它们更难遵循

这就是函数

    public ListNode switchPairs(){
        if (this==null || this.next==null)
            return this;
        ListNode top = this.next;

        ListNode first = this;
        ListNode second = first.next;

        do {
            ListNode third = second.next;
            second.next = first;
            first.next = third;
            first = third;
            System.out.println("@@@ " + top.toString());
            if (first != null) {
                // remember second now is really the first element on the list
                // at this point.
                second.next.next = first.next;
                second = first.next;
            }

        } while(first != null && second != null);

        return top;
    }
这就是全部代码

public class ListNode {
    private ListNode next = null;
    private final int i;
    ListNode(int i) {
        this.i = i;
    }

    ListNode(int i, ListNode parent) {
        this(i);
        parent.next = this;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("[" + this.i + "]");
        if (this.next != null) {
            sb.append("->");
            sb.append(this.next.toString());
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        ListNode top = null;
        ListNode curr = null;
        for(String arg : args) {
            int i = Integer.parseInt(arg);
            if(curr == null) 
                curr = new ListNode(i);
            else
                curr = new ListNode(i, curr);
            if( top == null) 
                top = curr;
        }
        System.out.println(top.toString());
        top = top.switchPairs();
        System.out.println(top.toString());
    }

    public ListNode switchPairs(){
        if (this==null || this.next==null)
            return this;
        ListNode top = this.next;

        ListNode first = this;
        ListNode second = first.next;

        do {
            ListNode third = second.next;
            second.next = first;
            first.next = third;
            first = third;
            System.out.println("@@@ " + this.toString());
            if (first != null) {
                second.next.next = first.next;
                second = first.next;
            }

        } while(first != null && second != null);

        return top;
    }
}
最后但并非最不重要的一个示例输出

java ListNode 1 2 3 4 5 6 7 8
[1]->[2]->[3]->[4]->[5]->[6]->[7]->[8]
@@@ [2]->[1]->[3]->[4]->[5]->[6]->[7]->[8]
@@@ [2]->[1]->[4]->[3]->[5]->[6]->[7]->[8]
@@@ [2]->[1]->[4]->[3]->[6]->[5]->[7]->[8]
@@@ [2]->[1]->[4]->[3]->[6]->[5]->[8]->[7]
[2]->[1]->[4]->[3]->[6]->[5]->[8]->[7]

我处理这个问题的方法是

  • 输入
    和所需的
    输出
    绘制
    链接列表
    ,格式正确,最简单。这里我将从4个节点开始

  • 然后处理一些简单的情况,例如
    ListNode
    next
    为空

  • 在纸上标记断开和形成的链接。请注意,您必须按正确的顺序进行断开和链接;确保有
    引用
    到要断开其链接的
    节点。否则,可能会丢失一些节点。这就是问题的症结所在。当节点断开或形成链接时,在每个步骤后绘制。通过这种方式,你可以跟踪正在发生的事情

  • 把你在纸上画的东西翻译成代码。那一定很简单! 通常需要临时指针来遍历列表

  • 在本例中,需要更改
    front
    head
    指针。因此,我将在迭代之外进行第一次交换。其余的更改将在
    while循环中执行

  • 编写一个方便的
    toString
    方法,可以帮助您跟踪每个阶段的变量。我发现使用
    调试器
    进行
    重复
    链接列表
    比较困难。但那只是我

  • 关于这个问题的解决办法:我认为这不是一个容易的问题。但这是一个很好的方法,可以很好地掌握
    链接列表和
    指针`

    以下是我的解决方案:

    public void switchPairs(){
        if (front==null || front.next==null)
            return ;
        //keep a pointer to next element of front
        ListNode current=front.next;
        //make front point to next element
        front.next=current.next;
        current.next=front;
        front=current;
        //current has moved one step back it points to first.
        //so get it to the finished swap position
        current=current.next;
        while(current.next!=null && current.next.next!=null){
            ListNode temp = current.next.next;
            current.next.next=temp.next;
            temp.next=current.next;
            current.next=temp;
            current=temp.next;
        }
    }
    

    我处理这个问题的方法是

  • 输入
    和所需的
    输出
    绘制
    链接列表
    ,格式正确,最简单。这里我将从4个节点开始

  • 然后处理一些简单的情况,例如
    ListNode
    next
    为空

  • 在纸上标记断开和形成的链接。请注意,您必须按正确的顺序进行断开和链接;确保有
    引用
    到要断开其链接的
    节点。否则,可能会丢失一些节点。这就是问题的症结所在。当节点断开或形成链接时,在每个步骤后绘制。通过这种方式,你可以跟踪正在发生的事情

  • 把你在纸上画的东西翻译成代码。那一定很简单! 通常需要临时指针来遍历列表

  • 在本例中,需要更改
    front
    head
    指针。因此,我将在迭代之外进行第一次交换。其余的更改将在
    while循环中执行

  • 编写一个方便的
    toString
    方法,可以帮助您跟踪每个阶段的变量。我发现使用
    调试器
    进行
    重复
    链接列表
    比较困难。但那只是我

  • 关于这个问题的解决办法:我认为这不是一个容易的问题。但这是一个很好的方法,可以很好地掌握
    链接列表和
    指针`

    以下是我的解决方案:

    public void switchPairs(){
        if (front==null || front.next==null)
            return ;
        //keep a pointer to next element of front
        ListNode current=front.next;
        //make front point to next element
        front.next=current.next;
        current.next=front;
        front=current;
        //current has moved one step back it points to first.
        //so get it to the finished swap position
        current=current.next;
        while(current.next!=null && current.next.next!=null){
            ListNode temp = current.next.next;
            current.next.next=temp.next;
            temp.next=current.next;
            current.next=temp;
            current=temp.next;
        }
    }
    

    公共静态链接列表开关对(链接列表){
    ListIterator迭代器=list.ListIterator();
    LinkedList out=null;
    while(iterator!=null&&iterator.hasNext()){
    if(out==null){
    out=新的LinkedList();
    }
    int temp=iterator.next();
    if(iterator.hasNext()){
    add(iterator.next());
    out.add(temp);
    }否则{
    out.add(temp);
    }
    }
    返回;
    
    }

    公共静态链接列表开关对(链接列表){
    
    // Recursive solution
    public void switchPairs(SingleLinkListNode prev, SingleLinkListNode node) {
    
        if (node == null || node.next == null) {
            return;
        }
        SingleLinkListNode nextNode = node.next;
        SingleLinkListNode temp = nextNode.next;
        nextNode.next = node;
        node.next = temp;
        if (prev != null) {
            prev.next = nextNode;
        } else {
            head = nextNode;
        }
    
        switchPairs(node, node.next);
    }
    
    ListIterator迭代器=list.ListIterator(); LinkedList out=null; while(iterator!=null&&iterator.hasNext()){ if(out==null){ out=新的LinkedList(); } int temp=iterator.next(); if(iterator.hasNext()){ add(iterator.next()); out.add(temp); }否则{ out.add(temp); } } 返回;
    }

    我有一个递归函数,它可以工作:

    // Recursive solution
    public void switchPairs(SingleLinkListNode prev, SingleLinkListNode node) {
    
        if (node == null || node.next == null) {
            return;
        }
        SingleLinkListNode nextNode = node.next;
        SingleLinkListNode temp = nextNode.next;
        nextNode.next = node;
        node.next = temp;
        if (prev != null) {
            prev.next = nextNode;
        } else {
            head = nextNode;
        }
    
        switchPairs(node, node.next);
    }
    
        public void swap2List(){
        root = swap2List(root);   //pass the root node
        }   
        private Node swap2List(Node current){
        if(current == null || current.next == null){
        return current;
        }
        else{
        Node temp = current;
        Node temp2 = current.next.next;
        current = current.next;
        current.next = temp;
        temp.next = swap2List(temp2);       
        } 
        return current;
        }
    

    我有一个递归函数,它可以工作:

        public void swap2List(){
        root = swap2List(root);   //pass the root node
        }   
        private Node swap2List(Node current){
        if(current == null || current.next == null){
        return current;
        }
        else{
        Node temp = current;
        Node temp2 = current.next.next;
        current = current.next;
        current.next = temp;
        temp.next = swap2List(temp2);       
        } 
        return current;
        }
    

    因此,它交换第一对,但不交换其余的。看起来你的循环不起作用了。您是否打印了变量以查看您正在修改正确的节点?这里有一些重复的代码。此代码中有两个区域正在尝试交换ListNodes。一个有效,o