Lintcode489-Convert Array List to Linked List-Easy
489. Convert Array List to Linked List
Convert an array list to a linked list.
Example
Example 1:
Input: [1,2,3,4],
Output: 1->2->3->4->null
定义两空指针,一个用来返回整个链表,一个用来创建新节点。 新创建的节点作为当前p的next节点,再把p重新指向新创建的节点。
public ListNode toLinkedList(List<Integer> nums) {
if (nums.size() == 0) {
return null;
}
ListNode head = null;
ListNode p = null;
for (Integer num : nums) {
// 创建头节点
if (head == null) {
head = new ListNode(num);
p = head;
} else {
p.next = new ListNode(num);
p = p.next;
}
}
return head;
Lintcode489-Convert Array List to Linked List-Easy的更多相关文章
- Java – How to convert Array to Stream
Java – How to convert Array to Stream 1. Object Arrayspackage com.mkyong.java8; import java.util.Arr ...
- LeetCode--LinkedList--206. Reverse Linked List(Easy)
206. Reverse Linked List(Easy) 题目地址https://leetcode.com/problems/reverse-linked-list/ Reverse a sing ...
- LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)
160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...
- 【leetcode】Convert Sorted Array to Binary Search Tree (easy)
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 有序 ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- Array List和Linked List实现分析
一,前言 先来一张Collection集合图. 今天分享一些关于Collection集合中的List,讲真的集合这东西在网上真是老生常谈了.说实话连本人都觉得腻了(哈哈),但是话又说回来,整个 ...
- 手工实现Array List和Linked List
Array List样例: /** * 增加泛型 * 自动增加数组容量 * 增加set.get方法:增加数组边界的检查 * 增加remove方法 */package cn.study.lu.four; ...
- LeetCode--Squares of a Sorted Array && Robot Return to Origin (Easy)
977. Squares of a Sorted Array (Easy)# Given an array of integers A sorted in non-decreasing order, ...
- 【leetcode】Intersection of Two Linked Lists(easy)
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
随机推荐
- wordCount剖析Spark模型
- js 转java后台传过来的list
var intIndex=0; arrList = new Array(); arrList = "${orderNumList}".replace('[','').replace ...
- STP(Spanning Tree Protocol)
STP生成树协议 问题 为了提高网络的可用性,需要进行冗余和备份.但是冗余路径会产生环路 环路会导致以下问题 广播风暴:由于交换机会对广播.多播.和未知目标MAC的单播包进行泛洪,在存在环路的情况 ...
- netcore log4相关
配置: 1:NuGet程序包 - 搜索log4net - 安装 2:配置代码 Startup文件 #region log4 public static ILoggerRepository ...
- what's the 灰盒测试
what's the 灰盒测试 灰盒测试的概念:是一种综合测试的方法,他将白盒测试和黑盒测试结合在一起,构成一种无缝测试技术. 灰盒测试的思想:是基于程序运行时的外部表现又结合程序内部逻辑结构来设计测 ...
- theano使用GPU踩坑
1.安装pygpu的部分 #使用豆瓣源or不使用,均安装失败 pip install pygpu -i http://pypi.douban.com/simple/ --trusted-host py ...
- CJSON在项目中的应用
无需编译,只需将 cJSON.c.cJSON.h 添加到项目中即可使用
- ubuntu 下dns一类的处理
如何关掉Ubuntu内置的dnsmasq服务 sudo vi /etc/NetworkManager/NetworkManager.conf找到dns=dnsmasq,在前面增加“#”,也就是把这句注 ...
- 微信网页浏览器打开链接后跳转到其他浏览器下载APK文件包
做微信营销活动或者APK下载推广时候,是无法直接下载,做到微信中正常使用呢?这就要借助一些工具来实现有效的操作. 安卓手机的话是通过点击链接,直接跳转出微信.自动打开手机默认的浏览器.但是这个方法IO ...
- MyBatis探究-----返回Map类型数据
1.使用@MapKey @MapKey:告诉mybatis封装Map的时候使用哪个属性作为Map的key Map<K, V>:键是这条记录的主键key,值是记录封装后的javaBean 1 ...