• 每日一题 为了工作 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();
    	}
    }
    

      

    *运行结果

     

  • 相关阅读:
    Android使用TextView实现无下划线超链接
    新浪微博Android SDK的使用
    android自定义View垂直滚动的TextView
    android开发问题集锦
    android实现TextView垂直或水平滚动
    自定义view,实现文本自动换行
    AndroidMenifest.xml中android:sharedUserId="android.uid.system权限访问sd卡问题
    【Android】报表、线图、饼图、柱状图 (工具整理)
    Python | 三种赋值(直接赋值,copy,deepcopy)
    Python | 三种赋值(直接赋值,copy,deepcopy)
  • 原文地址:https://www.cnblogs.com/walxt/p/12659213.html
Copyright © 2020-2023  润新知