Merge Two Sorted Lists & Remove Nth Node From End of List
1.合并两个排好序的list
Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
2.删除list倒数第n个元素
Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.
两个问题合在一个工程里面测试
package com.rust.datastruct;
public class MergeTwoSortedLists {
public static ListNode mergeTwoSortedLists(ListNode l1, ListNode l2) {
ListNode r1 = new ListNode(0);
ListNode res = r1;
ListNode t1 = l1;//java 中这样的赋值操作,对了l1操作等同于对t1操作
ListNode t2 = l2;
while (t1 != null && t2 != null){
if (t1.val <= t2.val) {
r1.next = t1;
t1 = t1.next;
} else {
r1.next = t2;
t2 = t2.next;
}
r1 = r1.next;
}
if (t1 != null) {
r1.next = t1;
}
if (t2 != null) {
r1.next = t2;
}
res = res.next;
return res;
}
/**
* @param head
* @param n
* @return ListNode
*/
public static ListNode removeNthFromEnd(ListNode head, int n) {
if (n == 0) return head;
ListNode fakeNode = head;
int count = 0;
while (fakeNode != null) {
fakeNode = fakeNode.next;
count++;
}
fakeNode = head;
if (n == count) {
head = head.next;
return head;
} else {
for (int i = 0; i < count; i++) {
if (i + n + 1== count) {
System.out.println(fakeNode.val);
ListNode cut = fakeNode.next.next;
fakeNode.next = cut;
count--;
continue;
}
fakeNode = fakeNode.next;
}
}
return head;
}
public static void main(String args[]){
ListNode l1 = new ListNode(0);
ListNode l2 = new ListNode(1);
ListNode p1 = l1;
ListNode p2 = l2;
/*initial the list*/
for (int i = 2; i <= 10; i++) {
if (i%2 == 0) {
p1.next = new ListNode(i);
p1 = p1.next;
} else {
p2.next = new ListNode(i);
p2 = p2.next;
}
}
p1 = l1;
p2 = l2;
System.out.println("input List l1 and l2");
showData(l1, l2);//after show,l1 and l2 value didn't change!
System.out.println("mergeTwoLists(l1, l2) -->");
ListNode res = mergeTwoSortedLists(l1, l2);
/** test mergeTwoSortedLists start ************/
while (res.next != null) {// res is destroyed
System.out.print(res.val + "\t");
res = res.next;
}
System.out.println(res.val);
System.out.println("After merge");
/** End ***********************************/
/** test removeNthFromEnd start **************/
showData(l1, l2);
// use l2 to test
int n = 1;
l2 = removeNthFromEnd(l2,n);
showData(l1, l2);
/** End ***********************************/
}
/**
* Print the ListNode
* @param l1 ListNode
* @param l2 ListNode
*/
public static void showData(ListNode l1, ListNode l2){
System.out.println("l1 -->");
while (l1.next != null) {
System.out.print(l1.val + "\t");
l1 = l1.next;
}
System.out.println(l1.val);
System.out.println("l2 -->");
while (l2.next != null) {
System.out.print(l2.val + "\t");
l2 = l2.next;
}
System.out.println(l2.val);
System.out.println("/************************************/");
}
}
//Definition for singly-linked list.
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
输出:
input List l1 and l2
l1 -->
0 2 4 6 8 10
l2 -->
1 3 5 7 9
/************************************/
mergeTwoLists(l1, l2) -->
0 1 2 3 4 5 6 7 8 9 10
After merge
l1 -->
0 1 2 3 4 5 6 7 8 9 10
l2 -->
1 2 3 4 5 6 7 8 9 10
/************************************/
9
l1 -->
0 1 2 3 4 5 6 7 8 9
l2 -->
1 2 3 4 5 6 7 8 9
/************************************/
Merge Two Sorted Lists & Remove Nth Node From End of List的更多相关文章
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- Merge Two Sorted Lists
Merge Two Sorted Lists https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked ...
- [Leetcode][Python]21: Merge Two Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...
- [Leetcode][Python]23: Merge k Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...
- LeetCode之“链表”:Merge Two Sorted Lists && Merge k Sorted Lists
1. Merge Two Sorted Lists 题目链接 题目要求: Merge two sorted linked lists and return it as a new list. The ...
- python 中的堆 (heapq 模块)应用:Merge K Sorted Lists
堆是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复提取队列中第一个作业并运行,因为实际情况中某些时间较短的任务将等待很长时间才能结束,或者某些不短 ...
- 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]
题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- LeetCode: Merge k Sorted Lists 解题报告
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- [Leetcode] Merge k sorted lists 合并k个已排序的链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思 ...
随机推荐
- java音视频编解码问题:16/24/32位位音频byte[]转换为小端序short[],int[],以byte[]转short[]为例
前言:Java默认采用大端序存储方式,实际编码的音频数据是小端序,如果处理单8bit的音频当然不需要做转换,但是如果是16bit或者以上的就需要处理成小端序字节顺序. 注:大.小端序指的是字节的存储顺 ...
- 【MyBatis源码解析】MyBatis一二级缓存
MyBatis缓存 我们知道,频繁的数据库操作是非常耗费性能的(主要是因为对于DB而言,数据是持久化在磁盘中的,因此查询操作需要通过IO,IO操作速度相比内存操作速度慢了好几个量级),尤其是对于一些相 ...
- python基础之数据类型/字符串/元组/列表/字典
Python 数据类型 数字类型: int整型,long 长整型(在python3.0里不区分整型和长整型).float浮点型:complex复数(python中存在小数字池:-5--257):布尔值 ...
- go 测试sort性能
package main import "fmt" import "os" import "flag" import "bufio ...
- Spring mvc 中使用 kaptcha 验证码
生成验证码的方式有很多,个人认为较为灵活方便的是Kaptcha ,他是基于SimpleCaptcha的开源项目.使用Kaptcha 生成验证码十分简单并且参数可以进行自定义.只需添加jar包配置下就可 ...
- Cordova各个插件使用介绍系列(一)—$cordovaSms发送短信
详情链接地址:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/cordova-1-cordovasms/ 这是调用手机发送短信的插件 ...
- java 父类构造器
当创建任何java对象时,程序总会首先调用系统的父类非静态初始化块(隐式执行)和父类构造器(从object开始(java程序中所有类的最终父类都是java.lang.Object类,使用语句super ...
- 第一次接触Axure
现在已经是凌晨4:21了,我的第一份Axure.RP文件终于接近尾声,我带着些许疲倦些许兴奋的状态写下这篇博客,记录我和Axure的初遇. 三天前,我加入了湖南大学金山俱乐部,参加了第一次 ...
- 简单轻量级的一套JS 类库(RapidDevelopmentFramework.JS)
1.最近好久没有更新自己的博客了,一直在考虑自己应该写一些什么.4.2日从苏州回到南京的路上感觉自己的内心些崩溃和失落,我就不多说了? 猛然之间我认为自己需要找一下内心的平衡.决定开发属于自己一套快速 ...
- 探索Windows命令行系列(2):命令行工具入门
1.理论基础 1.1.命令行的前世今生 1.2.命令执行规则 1.3.使用命令历史 2.使用入门 2.1.启动和关闭命令行 2.2.执行简单的命令 2.3.命令行执行程序使用技巧 3.总结 1.理论基 ...