首页 > 编程知识 正文

链表题目,java二维数组初始化赋值

时间:2023-05-04 18:05:26 阅读:146652 作者:2008

二维数组初始化:

定义一: intn [3] [3]={ 0,0,0 },{ 0,0,0 },{ 0,0,0 }; 定义2:int [ ] [ ] arr={ 1,2,3 },{ 4,5,6 }; 定义3:int [ ] [ ] arr1=new int [ ] [ ] { 1,2 },{ 2,3 },{ 4,5 }; 一维数组定义: int[] a=new int[3]; 直接赋值创建对象: int[]b={1、2、3}; new初始化创建对象: int [ ] c=new int [ ] { 1,2,3 };24. 两两交换链表中的节点

以leetcode24题为例,进行Idea的手动赋值输入和输出:

给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。

你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

示例1 :输入: head=[ 1,2,3,4 ]输出: [ 2,1,4,3 ]示例2 :输入: head=[]输出: []示例3 :输入: head=[1]输出: [1]提示:莉莉

package com.haijiao 12138.demo.leet code.test 0817;/* * @ author : haijiao 12138 * @ class name : solution1* @ description : todo * @ date :2021/8/171833601 */17 listnode(listnode ) intval ) { this.val=val; }listnode(intval,ListNode next ) { this.val=val; this.next=next; } publicstaticvoidmain (字符串[ ] args ) { ListNode head1=new ListNode ); head1.val=1; ListNode head2=new ListNode (; head2.val=2; ListNode head3=new ListNode (; head3.val=3; ListNode head4=new ListNode (; //1 2 3 4 head4.val=4; head1.next=head2; head2.next=head3; head3.next=head4; head4.next=null; 列表节点列表节点=swap pairs (head1); //system.out.println(listnode ); //listnode listnode=swap pairs (head ); wile(listnode.next!=null(system.out.println ) listnode.val; listNode=listNode.next; } system.out.println (listnode.val; //system.out.println(listnode ); } publicstaticlistnodeswappairs (listnode head ) if ) head==null||head.next==null ) { return head; } ListNode next=head.next; head.next=swappairs(next.next ); next.next=head; 返回下一步; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。