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

时间:2017-02-03 15:07:34

标签: java linked-list

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)

该方法的逻辑似乎与我一致,但它不起作用。

2 个答案:

答案 0 :(得分:0)

这是有效的:

    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();
    }
}

答案 1 :(得分:0)

在下面的代码中,我假设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
    }

}