簡體   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