Solutions and Summay for Linked List Naive and Easy Questions
1.Remove Linked List Elements
package linkedlist;
/*
* Question: Remove all elements from a linked list of integers that have value val.
*/
public class RemoveLinkedListElements { /**
* @param head a ListNode
* @param val an integer
* @return a ListNode
*/
public static ListNode removeElements(ListNode head, int val) {
// Write your code here
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
dummy.next = head;
while(cur.next != null){
if(cur.next.val == val){
cur.next = cur.next.next;
}
else{
cur = cur.next;
}
}
return dummy.next;
}
}
2.Add Two Numbers
3.Delete Node in the Middle of Singly Linked List
4.Insertion Sort List
5.Merge Two Sorted Lists
6. Nth to Last Node in List
/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param head: The first node of linked list.
* @param n: An integer.
* @return: Nth to last node of a singly linked list.
*/
ListNode nthToLast(ListNode head, int n) {
// write your code here
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode walker = dummy;
ListNode runner = dummy;
while(runner.next != null && n>0){
runner = runner.next;
n--;
}
while(runner.next != null){
runner = runner.next;
walker = walker.next;
}
return walker.next;
}
}
7.Partition List
8.Remove Duplicates from Sorted List
9.Remove Nth Node From End of List
10.Reverse Linked List
11.Swap Nodes in Pairs
Solutions and Summay for Linked List Naive and Easy Questions的更多相关文章
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 141. Linked List Cycle【easy】
141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...
- LeetCode--LinkedList--141.Linked List Cycle(Easy)
141. Linked List Cycle(Easy)2019.7.10 题目地址https://leetcode.com/problems/linked-list-cycle/ Given a l ...
- 【leetcode】Remove Linked List Elements(easy)
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- 203. Remove Linked List Elements【Easy】【未排序链表删除其中的给定值】
Remove all elements from a linked list of integers that have value val. Example: Input: 1->2-> ...
- 141. Linked List Cycle【Easy】【判断链表是否存在环】
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- *92. Reverse Linked List II (follow up questions)
Reverse a linked list from position m to n. Do it in one-pass and in-place Note: 1 ≤ m ≤ n ≤ length ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- LeetCode题目按公司分类
LinkedIn(39) 1 Two Sum 23.0% Easy 21 Merge Two Sorted Lists 35.4% Easy 23 Merge k Sorted Lists 23.3% ...
随机推荐
- input输入限制
1:只能输入两位小数点:function keepTwoPointNum(that){ var val=that.value; if(isNaN(val)){ $(that).val(''); ret ...
- LVM逻辑卷疑问?
创建完逻辑卷后,删除以/dev/vdb1和/dev/vdb2为基础的分区后,逻辑卷依然生效???
- Eclipse 中Git的使用及如何解决冲突
1. 如何导入已有Git项目 1.1 File——>import… 出现以下界面 1.2 找到Git,然后双击‘Project from Git.或者点击next 1.3 双击Clone URI ...
- 详解vue组件的is特性:限制元素&动态组件
在vue.js组件教程的一开始提及到了is特性 意思就是有些元素,比如 ul 里面只能直接包含 li元素,像这样: <ul> <li></li> </ul&g ...
- 楚乔传 Princess Agents
英文片名是:Princess Agents 公主特工,哈哈,女主角是公主...所有隐藏的线索都暴露了...这么搞笑呢.
- 前端 跨Area后Cookie无法访问
创建两个区域,一个是User,一个是Manage. User区域有两个页面,index1,和index2 User区域: index1:负责写入cookie index2:负责读取cookie Man ...
- 2018面向对象程序设计(Java)第18周学习指导及要求
2018面向对象程序设计(Java) 第18周学习指导及要求(2018.12.27-2018.12.30) 学习目标 (1) 综合掌握java基本程序结构: (2) 综合掌握java面向对象程序设 ...
- EasyUI DataGrid设置列宽为百分比导致表头和内容错位的解决方法
在DataGrid中设置列宽为百分比一般是没有问题的 columns: [[{ title: '内容', field: '__EMPTY', width: '40%' }, { title: '隐患级 ...
- 245. Shortest Word Distance III 单词可以重复的最短单词距离
[抄题]: Given a list of words and two words word1 and word2, return the shortest distance between thes ...
- jenkins shell部署
jenkins执行shell脚本 jenkins执行shell 上一篇说的是jenkins+svn+maven把war包自动部署到Tomcat,这篇是从SVN上拉取代码maven生成jar文件,并且拷 ...