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. TypeScript 零基础入门

    前言 2015 年末看过一篇文章<ES2015 & babel 实战:开发 npm 模块>,那时刚接触 ES6 不久,发觉新的 ES6 语法大大简化了 JavaScript 程序的 ...

  2. 【JAVAWEB学习笔记】网上商城实战5:后台的功能模块

    今日任务 完成后台的功能模块 1.1      网上商城的后台功能的实现: 1.1.1    后台的功能的需求: 1.1.1.1  分类管理: [查询所有分类] * 在左侧菜单页面中点击分类管理: * ...

  3. 每天一个JS 小demo之韩雪冬轮播图。主要知识点:html,css布局,对于数组和对象的理解和运用

    @charset "utf-8"; /* CSS Document */ ;; } li { list-style: none; } img { border: none; } b ...

  4. ionic2新手入门整理,搭建环境,创建demo,打包apk,热更新,优化启动慢等避坑详解

    onic官方文档链接:http://ionicframework.com/docs/ 如果是新的环境会有很多坑,主要是有墙,请仔细阅读每个步骤 文档包含以下内容: l  环境搭建 l  创建demo并 ...

  5. String 类问题发现与解决

    1.在代码中出现:String t = null; t.length(); 执行后:控制台报:java.lang.NullPointerException 原因:Java中,null是一个关键字,用来 ...

  6. 学习总结------Servlet控制器的简单运用

    前言: 今天将简单的模拟 MVC模式 对Servlet控制器运行 若有不好或不对的地方,欢迎各位大神进行指导! 1.MVC模式(图) MVC全名是Model View Controller,是模型(m ...

  7. Scrapy爬虫大战京东商城

    Scrapy爬虫大战京东商城 引言 上一篇已经讲过怎样获取链接,怎样获得参数了,详情请看python爬取京东商城普通篇 代码详解 首先应该构造请求,这里使用scrapy.Request,这个方法默认调 ...

  8. js判断密码强度是否符合

    /** 判断密码强度是否符合 */ function check_passwd_intensity(password) { value = $.trim(password); if( value.le ...

  9. python中的字符串编码

    获取字符串的编码类型: encodingdate = chardet.detect(str) chardet用于实现字符串的编码类型检测 chardet的下载地址:https://pypi.pytho ...

  10. 如何使用LIBSVM,从安装到基本实例使用

    1.在eclipse上安装libsvm 下载libsvm压缩包解压到本地目录,下载地址http://www.csie.ntu.edu.tw/~cjlin/libsvm/index.html 如图: 2 ...