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% ...
随机推荐
- java 调用 python 的几种方法整理
参考: https://blog.csdn.net/secondlieutenant/article/details/79000265
- js 模拟css3 动画3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- ABAP-FTP-执行
1.界面 2.程序 ZFID0004_FTP_EXEC 主程序: *&------------------------------------------------------------- ...
- 干货|技术小白如何在45分钟内发行通证(TOKEN)并上线交易(附流程代码
https://blog.csdn.net/HiBlock/article/details/80071478
- oracle 连接字符串的问题
未指定的错误,发生了一个 Oracle 错误,但无法从 Oracle 中检索错误信息.数据类型不被支持. 原因是你用的ADO for ORACLE的驱动是微软的Microsoft OLE DB ...
- 3. Go语言基本类型
Go语言基本类型如下: bool string 数值类型 (int8, int16, int32, int64, int, uint8, uint16, uint32, uint64, uint, f ...
- mysql学习笔记--数据库操作
一.显示数据库 show databases; 二.创建数据库 create database [if not exists] 数据库名 [字符编码] 注意: a. 如果已经存在数据库再创建会报错 b ...
- IIC基本概念和基本时序
1. IIC基本概念和基本时序 1.1 I2C串行总线概述 I2C总线是PHLIPS公司推出的一种串行总线,是具备多主机系统所需的包括总线裁决和高低速器件同步功能的高性能串行总线. 1.I2C总线具有 ...
- Centos7下安装Docker[z]
[z]https://www.cnblogs.com/qgc1995/p/9553572.html https://yq.aliyun.com/articles/691610?spm=a2c4e.11 ...
- MySQL优化(三) 表的设计
1.什么样的表才符合3范式(3 NF)? 表的范式,是首先符合1范式,才能满足2范式,进一步才能满足3范式:(现在最高级别是6范式) 第一范式:1NF 是对属性的原子性约束,要求表的属性(列)具有原子 ...