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. FPGA设计思想(持续更新)

    一. 流水线设计 将原本一个时钟周期完成的较大的组合逻辑通过合理的切割后分由多个时钟周期完成.该部分逻辑运行的时钟频率会有明显对的提升,提高系统的性能用面积换速度 一个流水线设计需要4个步骤完成一个数 ...

  2. 提高java编程质量 - (四)i++ 和 ++i 探究原理

    先看一个例子: package com.test; public class AutoIncrement { public static void main(String[] args) { int ...

  3. 页面实现多个定时器(计时器)时选用NSTimer还是GCD?(干货不湿)

    定时器在我们每个人做的iOS项目里面必不可少,如登录页面倒计时.支付期限倒计时等等,一般来说使用NSTimer创建定时器: + (NSTimer *)timerWithTimeInterval:(NS ...

  4. nodejs 字符串全排列 和 去重

    以前写了个java版的 现在写个nodejs 版的 var list = sort('CCAV');var noRepeat = {};for(var i in list){ noRepeat[lis ...

  5. XCOM2中敌对生物设计分析(ADVENT篇)

    最近,在制作游戏Demo--DroneAssmble的过程中,对于敌对生物的设计,参考了幽浮系列的相关设定,因此着手对幽浮2中的主要敌人进行分析. 我们知道, XCOM2中的敌对生物主要由" ...

  6. MYSQL数据类型和where条件

    MySQL中常见的数据类型 一.字符型 ① CHAR(N):固定N个字符长度的字符串,如果长度不够自动空格补齐; N的范围 0~255 ② VARCHAR(N): 存储可变长度的字符串,最常用 ③ T ...

  7. 持续集成篇-- SonarQube代码质量管理平台的配置与使用

    样例视频教程:http://www.roncoo.com/course/view/85d6008fe77c4199b0cdd2885eaeee53 一.SonarQube的配置(前提,先用admin用 ...

  8. shell中source与sh区别

    shell中使用source conf.sh,是直接运行conf.sh的命令,不创建子shell,类似与html中include,而sh是则创建子shell, 子shell里面 的变量父shell无法 ...

  9. 2017全球互联网技术大会回顾(附PPT)

    有幸遇见 GITC2017上海站,刚好遇见你! 为期两天(6.23~24)的GITC大会在上海举行,我有幸参加了24号的那场,也就是上周六,之所以今天才来回顾,是我想等PPT出来后分享给大家! 这应该 ...

  10. 分页插件Jpages的使用

    项目原因需要前端做分页表格,之前做了一个ul的分页效果,但是感觉自己写还是造轮子了,今天网上看到Jpqges插件就试了下,感觉平时使用挺方便的,写一下自己的使用过程. 先上套图,下载下来就2个js和1 ...