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 ...
随机推荐
- luogu4643 [国家集训队]阿狸和桃子的游戏
题目链接:洛谷 这道题乍一看非常的难,而且题目标题上的标签让人很害怕. 但其实这道题并不难写(只要想到了...emm) 因为我们只需要知道两个人得分之差,所以我们可以对条件进行变换. 我们将边权平分到 ...
- centos禁ping
Linux默认是允许Ping响应的,系统是否允许Ping由2个因素决定的:A.内核参数,B.防火墙,需要2个因素同时允许才能允许Ping,2个因素有任意一个禁Ping就无法Ping. 具体的配置方法如 ...
- service order
1: SRVO Service Order compress: 本地部署,客户能够看到后台代码,transaction type,改代码等. SVO1 Service Order ...
- 安全相关及HttpClient
1,Spring Security入门示例 Spring Security Annotation Configuration Example – HelloWorld 2,程序模块Get请求,获取响应 ...
- Python socket的客户端
做一个socket客户端1.声明一个实例2.绑定端口号和地址3.循环发送和接收响应其中要注意粘包的产生,为了防止粘包的产生,应该在服务器端先测出要发送信息的大小,然后发送响应至客户端,等到服务器上一条 ...
- 设置和获取cookie
通过make_response(“响应体”)创建response响应对象. 然后返回. 与直接return “响应体” 是一样的. 但是这里我们需要用到response响应对象,去设置cookie @ ...
- ultiple Endpoints may not be deployed to the same path
@Configurationpublic class WebSocketConfig { //打war包启动需要注释掉此:否则报 :DeploymentException: Multiple Endp ...
- 线性二次型调节器LQR/LQC算法解析及求解器代码(matlab)
参考链接:http://120.52.51.14/stanford.edu/class/ee363/lectures/dlqr.pdf 本文参考讲义中的第20页PPT,根据Hamilton-Jacob ...
- (转载)WinRAR离购买许可只剩x天
在WinRAR的安装目录下,新建文件RarReg.key(C:\Program Files\WinRAR\RarReg.key) 粘贴以下内容即可: RAR registration dataFede ...
- MAVEN简介之——settings.xml
概述 Maven的settings.xml配置了Maven执行的方式,像pom.xml一样,但是它是一个通用的配置, 不能绑定到任何特殊的项目.它通常包括本地仓库地址,远程仓库服务,认证信息等. se ...