【大厂算法面试冲刺班】day1:数据结构先导课-链表、列表

发布时间:2024年01月10日

链表

/*链表结点类*/
class ListNode{
	int val; //结点值
	ListNode next; //指向下一结点的指针(引用)
	ListNode(int x){val = x;} //构造函数
}

在链表中查找值为target的首个结点

int find(ListNode head, int target){
	int index = 0;
	while(head != null){
	if(head.val == target)
		return index;
	head = head.next;
	index++;
	}
	return -1;
}

列表

//无初始值初始化列表
List<Integer> list1 = new ArrayList<>();
//有初始值,注意数组的元素类型需为int[]的包装类Integer[]
Integer[] numbers = new Integer[] {1, 3, 2, 5, 4};
List<Integer> list = new ArrayList<>(Arrays.asList(numbers));
文章来源:https://blog.csdn.net/gyf200708/article/details/135515189
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。