繁体   English   中英

为什么不交换LinkedList的相邻对?

[英]Why doesn't this swap adjacent pairs of a LinkedList?

public void swapPairs() {
    ListNode temp = this;
    ListNode dummy = this;

    while (temp.next != null) {
        temp.next = temp.next.next;
        dummy.next.next = temp;
        temp = temp.next;
        dummy = dummy.next;
    }
}

我基本上是想在LL头节点上使用此方法,并让它交换相邻的对,使得(1,2,3,4)->(2,1,4,3)

该方法的逻辑对我来说似乎是一致的,但是它不起作用。

这正在工作:

    public void swap(){
        ListNode node1;
        ListNode node2;
        ListNode prev;
        prev = this;
        while(true){
            node1 = prev.next;
            if(node1 == null)
                break;
            node2 = node1.next;
            if(node2 == null)
                break;
            node1.next  = node2.next;
            node2.next  = node1;
            prev.next   = node2;
            prev        = node1;
        }
    }

完整程序:

package x;
class ListNode{
    ListNode next;
    int data;
    ListNode(){
        data = -1;
    }
    ListNode(int d){
        data = d;
    }
    public void append(int data) {
        ListNode newNode = new ListNode (data);
        if(this.next == null){
            this.next = newNode;
            return;
        }
        ListNode curr = this.next;
        while(curr.next != null)
            curr = curr.next;
        curr.next = newNode;
    }
    public void insert(int data) {
        ListNode newNode = new ListNode (data);
        newNode.next = this.next;
        this.next = newNode;
    }
    public void show(){
        ListNode node = this.next;
        while(node != null){
            System.out.println(node.data);
            node = node.next;
        }
        System.out.println();
    }
    public void swap(){
        ListNode node1;
        ListNode node2;
        ListNode prev;
        prev = this;
        while(true){
            node1 = prev.next;
            if(node1 == null)
                break;
            node2 = node1.next;
            if(node2 == null)
                break;
            node1.next  = node2.next;
            node2.next  = node1;
            prev.next   = node2;
            prev        = node1;
        }
    }
}


public class x {

    public static void main(String[] args) {
        ListNode head = new ListNode();
        head.append(4);
        head.append(5);
        head.append(6);
        head.append(7);
        head.insert(3);
        head.insert(2);
        head.insert(1);
        head.insert(0);
        head.show();
        head.swap();
        head.show();
    }
}

在下面的代码中,我假设this是指向头节点的指针:

public void swapAdjacentNodes() {

    ListNode first = this;  // point first to head node

    if (first == null) {
        return;
    }


    ListNode prev = null; // Node behind 1st adjacent node

    ListNode second = first.next; // second points to the 2nd adj node

    while (second != null) {  // There are still pairs to swap

        first.next = second.next;  // Set 1st adj next to 3rd (or null)
        second.next = first;       // Set 2nd adj next to 1st

        if (prev==null) {  // If 1st adj was 1st in list
            this = second;   // point head to 2nd adj
        } else {
            prev.next = second;  // point prev next to new 1st of pair.
        }
        prev = first;            // prev now points to new 1st of pair. 
        first = first.next;        // move cur to 1st of next pair
        if (first==null) {       // if there's no pair, we're done
            break;
        }
        second = first.next;       // Point second to 2nd of next pair
    }

}

暂无
暂无

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

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