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% ...
随机推荐
- 从零开始实现RPC框架 - RPC原理及实现
最近被人问到RPC相关的东西~突然发现还是有很多原理没有清楚,所以要好好系统的学习一下RPC以及它的原理 先大致了解一下RPC的大概,原文:https://blog.csdn.net/top_code ...
- Android APK反编译(一)
apk是安卓工程打包的最终形式,将apk安装到手机或者模拟器上就可以使用APP.反编译apk则是将该安卓工程的源码.资源文件等内容破解出来进行分析. 一.APK反编译基本原理 1.APK分析 apk文 ...
- python中os模块
os 模块 操作系统模块,该模块主要处理与操作系统相关的操作 最常用是文件操作:打开.读取 import os os.getcwd() #获取当前执行文件夹路径 os.chdir('dirnam ...
- MySQL存储过程定义中的特性(characteristic)的含义
MySQL的存储过程蛮啰嗦的,与MSSQL或者Oracle的存储过程相比,如果没有显式指定,他会隐含地指定一系列特性(characteristic)的默认值来创建存储过程 通常在使用图形界面工具进行存 ...
- Python中*和**的作用(课堂小结)
以前自学没注意过参数的传导中*和**的用法,这次趁着上课了解了一下,顺便写个随笔记一下. 1.打包用法 在参数传导中*args是不定长参数,传入的参数是不限制个数的,比如 def bdc(*args) ...
- mysql修改EST时区,mysql时间修改
方法有两种 ###第一种 select NOW();show variables like "%time_zone%"; ##一:通过sql命令临时修改 set global ti ...
- NLTK 统计词频
import nltk Freq_dist_nltk = nltk.FreqDist(list) for k,y in Freq_dist_nltk: print str(k),str(y)
- ExecuteReader()获得数据
ExecuteReader用于实现只进只读的高效数据查询.ExecuteReader:返回一个SqlDataReader对象,可以通过这个对象来检查查询结果,它提供了只进只读的执行方式,即从结果中读取 ...
- 450. Delete Node in a BST 删除bst中的一个节点
[抄题]: Given a root node reference of a BST and a key, delete the node with the given key in the BST. ...
- java_22 Map接口
1Map Collection是孤立存在的,向集合中存储元素是一个一个放进去的 Map中的集合存储是成对的,可以通过键找到值.即将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值 ...