简体   繁体   English

给定一个链表 i & j,交换 LL 中 i & j 位置的节点。 您需要交换整个节点,而不仅仅是数据

[英]Given a linked list, i & j, swap the nodes that are present at i & j position in the LL. You need to swap the entire nodes, not just the data

I am getting this error:我收到此错误:

Compilation Failed
./Solution.java:33: error: cannot find symbol
        while (node1 != null && node1.val != i) {
                                     ^
  symbol:   variable val
  location: variable node1 of type LinkedListNode
./Solution.java:40: error: cannot find symbol
        while (node2 != null && node2.val != j) {
                                     ^
  symbol:   variable val
  location: variable node2 of type LinkedListNode

Expected Output预期产出

3 4 5 6 2 1 9

public class Solution {



public static  LinkedListNode<Integer> swap_nodes(LinkedListNode<Integer> head,int i,int j){
        if (i == j)
            return head;

       LinkedListNode<Integer> prev1  = null, node1 = null, prev2 = null, node2 = null;

        //Search for the first node
        node1 = head;
        while (node1 != null && node1.val != i) {
            prev1 = node1;
            node1 = node1.next;
        }

        //Search for the second node
        node2 = head;
        while (node2 != null && node2.val != j) {
            prev2 = node2;
            node2 = node2.next;
        }

        if (node1 == null || node2 == null)
            return head;

        LinkedListNode<Integer>  tail1 = node1.next, tail2 = node2.next;

        if (node2 == node1.next) {
            if (prev1 != null)
                prev1.next = node2;
            node2.next = node1;
            node1.next = tail2;
        }
        else if(node1 == node2.next) {
            if (prev2 != null)
                prev2.next = node1;
            node1.next = node2;
            node2.next = tail1;
        }
        else {
            if (prev1 != null)
                prev1.next = node2;
            node2.next = tail1;
            if (prev2 != null)
                prev2.next = node1;
            node1.next = tail2;
        }

        if (node1 == head)
            head = node2;
        else if (node2 == head)
            head = node1;

        return head;
    }
}
 public class Solution {
    public static  LinkedListNode<Integer> swap_nodes(LinkedListNode<Integer> head,int i,int j){
        if (i == j)
            return head;

       LinkedListNode<Integer> prev1  = null, node1 = null, prev2 = null, node2 = null;

        //Search for the first node
        int count1 = 0;
        int count2=0;
        node1 = head;
        while (node1 != null && count1 != i) {
            prev1 = node1;
            node1 = node1.next;
            count1++;
        }

        //Search for the second node
        node2 = head;
        while (node2 != null && count2 != j) {
            prev2 = node2;
            node2 = node2.next;
            count2++;
        }

        if (node1 == null || node2 == null)
            return head;

        LinkedListNode<Integer>  tail1 = node1.next, tail2 = node2.next;

        if (node2 == node1.next) {
            if (prev1 != null)
                prev1.next = node2;
            node2.next = node1;
            node1.next = tail2;
        }
        else if(node1 == node2.next) {
            if (prev2 != null)
                prev2.next = node1;
            node1.next = node2;
            node2.next = tail1;
        }
        else {
            if (prev1 != null)
                prev1.next = node2;
            node2.next = tail1;
            if (prev2 != null)
                prev2.next = node1;
            node1.next = tail2;
        }

        if (node1 == head)
            head = node2;
        else if (node2 == head)
            head = node1;

        return head;
    }
}

Your logic is correct, but you are comparing the nodes, which produces the error.您的逻辑是正确的,但您正在比较节点,这会产生错误。

while (node1 != null && node1.val != i)

i and j are indices of nodes to be swapped, and not the values of the node themselves, the rest of your concept is correct. ij是要交换的节点的索引,而不是节点本身的值,其余概念是正确的。

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

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