成对交换单链表的结点

package algorithm;


//【024-Swap Nodes in Pairs(成对交换单链表的结点)】
//https://blog.csdn.net/derrantcm/article/details/47034975
/*
原题
  Given a linked list, swap every two adjacent nodes and return its head. 
  For example, 
  Given 1->2->3->4, you should return the list as 2->1->4->3. 
  Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed. 

题目大意
  给定一个单链表,成对交换两个相邻的结点。算法法应该做常量辅助空间,不能改结点的值,只能交换结点。 

*/
public class SwitchDouble {

    public class Node{
        
        int value;
        Node nextNode;
        public Node(int value) {
            this.value=value;
            nextNode=null;
        }
    }
    
    public Node createNode(int[] tmp){
        if(tmp==null)
            return null;
        Node root=null;
        Node first=null;
        for(int i=0;i<tmp.length;i++){
            Node tmpNode=new Node(tmp[i]);
            if(i==0){
                first=tmpNode;
                root=tmpNode;
            }else{
                
                root.nextNode=tmpNode;
                root=tmpNode;
            }
        }
        return first;
    }
    
    /*
     * 遍历
     * */
    public void getNodeValues(Node node){
        
        Node root=node;
        while(node!=null){
          System.out.println(node.value);
          node=node.nextNode;
        }
        node=root; //恢复指向
    }
    
    //相邻接口的交换
    public Node switchNode(Node node){
      
        if(node==null){
            return null;
        }
        Node root=null;
        Node tmp1=node;
        Node tmp2=node.nextNode;
        int circle=0;
        while(tmp2!=null){
            circle++;
            if(circle==1){
                root=tmp2;
            }
            tmp1.nextNode=tmp2.nextNode;
            tmp2.nextNode=tmp1;
            if(circle!=1){
                node.nextNode=tmp2;    
            }
            
            node=tmp1;
            
            System.out.println(node+"  "+tmp1+tmp2);
            
            tmp1=node.nextNode;
            if(tmp1==null){
                break;
            }
            tmp2=node.nextNode.nextNode;
            System.out.println(node+"  "+tmp1+tmp2);
        }
        
        return root;
        
    }
    
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] ints={1,2,3,4,5};
      Node node= new SwitchDouble().createNode(ints);
        
       new SwitchDouble().getNodeValues(node);
      Node node1= new SwitchDouble().switchNode(node);
       System.out.println(node1);

       System.out.println(node1);
    }

}
--------------------------------------------------------------------------------------------------------

或者

 

package algorithm;


//【024-Swap Nodes in Pairs(成对交换单链表的结点)】
//https://blog.csdn.net/derrantcm/article/details/47034975
/*
原题
  Given a linked list, swap every two adjacent nodes and return its head.  
  For example,  
  Given 1->2->3->4, you should return the list as 2->1->4->3.  
  Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.  

题目大意
  给定一个单链表,成对交换两个相邻的结点。算法法应该做常量辅助空间,不能改结点的值,只能交换结点。  

*/
public class SwitchDouble {

  public class Node{
    
    int value;
    Node nextNode;
    public Node(int value) {
           this.value=value;
           nextNode=null;
    }
  }
  
  public Node createNode(int[] tmp){
    if(tmp==null)
           return null;
    Node root=null;
    Node first=null;
    for(int i=0;i<tmp.length;i++){
           Node tmpNode=new Node(tmp[i]);
                  if(i==0){
                first=tmpNode;
                root=tmpNode;
              }else{
                
                root.nextNode=tmpNode;
                root=tmpNode;
              }
    }
    return first;
  }
  
  /*
   * 遍历
   * */
  public void getNodeValues(Node node){
    
    Node root=node;
    while(node!=null){
       System.out.println(node.value);
       node=node.nextNode;
    }
    node=root; //恢复指向
  }
  
  //相邻接口的交换
  public void switchNode(Node node){
    //  public Node switchNode(Node node){
     
    if(node==null){
        //   return null;
        return;
    }
    Node root=null;
    Node tmp1=node;
    Node tmp2=node.nextNode;
    int circle=0;
    while(tmp2!=null){
           circle++;
           if(circle==1){
             root=tmp2;
           }
           tmp1.nextNode=tmp2.nextNode;
           tmp2.nextNode=tmp1;
           if(circle!=1){
             node.nextNode=tmp2;  
           }
           
           node=tmp1;
                   
           System.out.println(node+"   "+tmp1+tmp2);
           
           tmp1=node.nextNode;
           if(tmp1==null){
             break;
           }
           tmp2=node.nextNode.nextNode;
           System.out.println(node+"   "+tmp1+tmp2);
    }
    
    
    node=root;
    System.out.println(node+" "+root);
    System.out.println(node+" "+root);
  //  return root;      
  }
  
  
  public static void main(String[] args) {
         //   int[] ints={1,2,3,4,5};
       int[] ints={1,2,3,4};
         Node node= new SwitchDouble().createNode(ints);
    
           new SwitchDouble().getNodeValues(node);
//         Node node1= new SwitchDouble().switchNode(node);
  //         System.out.println(node1);
          new SwitchDouble().switchNode(node);
           System.out.println(node);

    System.out.println(node);
  }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值