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的更多相关文章

  1. [LeetCode] Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...

  2. Merge Two Sorted Lists

    Merge Two Sorted Lists https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked ...

  3. [Leetcode][Python]21: Merge Two Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...

  4. [Leetcode][Python]23: Merge k Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...

  5. 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 ...

  6. python 中的堆 (heapq 模块)应用:Merge K Sorted Lists

    堆是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复提取队列中第一个作业并运行,因为实际情况中某些时间较短的任务将等待很长时间才能结束,或者某些不短 ...

  7. 蜗牛慢慢爬 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 ...

  8. LeetCode: Merge k Sorted Lists 解题报告

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  9. [Leetcode] Merge k sorted lists 合并k个已排序的链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思 ...

随机推荐

  1. JavaSE教程-03Java中分支语句与四种进制转换-练习

    练习1: 老师如果带100或以上则请客吃饭 import java.util.Scanner; public class Test2{ public static void main(String[] ...

  2. linux 系统备份日志

    题目: 备份日志 小明是一个服务器管理员,他需要每天备份论坛数据(这里我们用日志替代),备份当天的日志并删除之前的日志.而且备份之后文件名是年-月-日的格式.alternatives.log在/var ...

  3. JavaMail API

    JavaMail API的核心类:会话.消息.地址.验证程序.传输,存储和文件夹.所有这些类都可以在JavaMail API即javax.mail的顶层包中找到,尽管你将频繁地发现你自己使用的子类是在 ...

  4. idea live template高级知识, 进阶(给方法,类,js方法添加注释)

    为了解决用一个命令(宏)给方法,类,js方法添加注释,经过几天的研究.终于得到结果了. 实现的效果如下: 给Java中的method添加方法: /** * * @Method : addMenu * ...

  5. ionic3 环境配置 + 運行第一個項目

    1.首先我們先下載個 nodejs安裝包 地址:https://nodejs.org/en/ 安裝 檢查是否安裝成功,打開命令提示符 輸入 node -v 回車 查看版本 結果: 2.安裝ionic ...

  6. JavaScript数组知识点

    强类型语言数组特点:连续的,指定好长度, 还要规定好数据类型弱类型语言数组特点:不一定是连续的 可以不用指定长度 不限定数据类型(可以存储任意类型的数据)数组定义方式:1.var arr=new Ar ...

  7. 开始学习机器学习,从Ng的视频开始

    时隔开5个月,忙完了考研和毕设后终于有时间搞自己想搞得,研究生导师方向是图像处理与机器学习结合,重新开工 何为机器学习? 对于机器学习(Machine Learning)的定义大体上有两种,第一种是美 ...

  8. [USACO4.2]草地排水Drainage Ditches

    题目背景 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草 要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水淹 ...

  9. java编码详解

    举个例子 我们在开发过程中,特别是多种编码格式并存的情况下,很容易遇到乱码问题. 假如有一个GBK编码java文件,然后再使用-Dfile.encoding=GBK参数,写入的文件中哪些是乱码呢.那如 ...

  10. PHP加密解密的函数

    <?php class Encryption { /* *功能:对字符串进行加密处理 *参数一:需要加密的内容 *参数二:密钥 */ function encrypt($str,$key){ / ...