zoukankan      html  css  js  c++  java
  • 每日一题 为了工作 2020 0408 第三十七题

    /**
     * 问题:
     * 	     按照左右半区的方式重新组合单链表
     *    给定一个单链表的头部节点 head, 链表长度为 N, 如果 N为偶数, 那么前 N/2个节点算
     * 作左半区, 后面 N/2个节点算作右半区; 如果 N为奇数, 那么前面 N/2个节点算作左半区,后
     * 面 N/2+1个节点算作右半区。左半区从左到右依次记为 L1->L2->…,右半区从左到右依次记
     * 为 R1->R2->…,请将单链表调整成 L1->R1->L2->R2->…的形式。
     * 
     * 例如:
     * 1->null, 调整为 1->null。
     * 1->2->null, 调整为 1->2->null。
     * 1->2->3->null, 调整为 1->2->3->null。
     * 1->2->3->4->null, 调整为 1->3->2->4->null。
     * 1->2->3->4->5->null, 调整为 1->3->2->4->5->null。
     * 1->2->3->4->5->6->null, 调整为 1->4->2->5->3->6->null。
     * 
     * 解答:
     * 1.如果链表为空或长度为 1,则不用调整,直接返回原链表。
     * 2.如果链表的长度大于一,遍历一遍找到左半区的最后一个节点,记为 mid。
     * 	  例如:1->2, mid为 1;
     * 		 1->2->3,mid为 1;
     *       1->2->3->4,mid为 2; 
     *       1->2->3->4->5,mid为 2; 
     *       1->2->3->4->5->6, mid为 3。
     *   也就是说, 从长度为 2开始,长度每增加 2, mid就往后移动一个节点。
     * 3.遍历一遍找到 mid之后,将左半区与右半区分离成两个链表,分别记为 left和 right。
     * 4.链表合并。
     * 
     * @author 雪瞳
     *
     */
    

      

    public class Node {
    	public int value;
    	public Node next;
    	public Node(int data){
    		this.value=data;
    	}
    }
    

      

    public class RelocateNode {
    
    	public Node relocate(Node head){
    		
    		if(head == null || head.next==null){
    			return head;
    		}
    		
    		Node mid = head;
    		Node current = head.next;
    		while(current.next != null && current.next.next != null){
    			mid = mid.next;
    			current = current.next.next;
    		}
    		Node left = head;
    		Node right = mid.next;
    		//切断链表
    		mid.next = null;
    		//重连链表
    		//test
    		TestRelocateNode test = new TestRelocateNode();
    		test.showNodeList(left);
    		test.showNodeList(right);
    		return changeLR(left, right);
    	}
    	public Node changeLR(Node left,Node right){
    		Node head = left;
    		Node leftNext = null;
    		Node rightNext = null;
    		while(left.next != null){
    			leftNext = left.next;
    			rightNext = right.next;
    			left.next = right;
    			right.next = leftNext;
    			left = leftNext;
    			right = rightNext;
    		}
    		left.next=right;
    		return head;
    	}
    }
    

      

    import java.util.Random;
    import java.util.Scanner;
    
    public class TestRelocateNode {
    	public static void main(String[] args) {
    		TestRelocateNode test = new TestRelocateNode();
    		RelocateNode relocate = new RelocateNode();
    		Scanner sc = new Scanner(System.in);
    		System.out.println("输入链表长度");
    		int len=0;
    		len =sc.nextInt();
    		Node head = test.getNodeList(len);
    		test.showNodeList(head);
    		Node result = relocate.relocate(head);
    		test.showNodeList(result);
    		sc.close();
    	}
    	public Node getNodeList(int length){
    		Random rand = new Random();
    		Node nodeArray[]= new Node[length];
    		for(int i=0;i<length;i++){
    			nodeArray[i]=new Node(rand.nextInt(10));
    		}
    		for(int i=0;i<length-1;i++){
    			nodeArray[i].next = nodeArray[i+1];
    		}
    		return nodeArray[0];
    	}
    	public void showNodeList(Node head){
    		Node current = null;
    		current = head;
    		System.out.println("链表元素如下...");
    		while(current!=null){
    			System.out.print(current.value+"	");
    			current=current.next;
    		}
    		System.out.println();
    	}
    }
    

      

    *运行结果

     

  • 相关阅读:
    P1121 环状最大两段子段和
    无题
    cdoj 1485 柱爷搞子串 sam treap
    自然数幂和
    Gym 100341C AVL Trees NTT
    线性筛分解质因子
    codeforces 366 Ant Man dp
    UVALive 6914 Maze Mayhem 轮廓线dp
    hdu 5790 Prefix 字典树 主席树
    莫比乌斯反演个人小结
  • 原文地址:https://www.cnblogs.com/walxt/p/12659213.html
Copyright © 2011-2022 走看看