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.

两个问题合在一个工程里面测试

  1 package com.rust.datastruct;
  2
  3 public class MergeTwoSortedLists {
  4     public static ListNode mergeTwoSortedLists(ListNode l1, ListNode l2) {
  5         ListNode r1 = new ListNode(0);
  6         ListNode res = r1;
  7         ListNode t1 = l1;//java 中这样的赋值操作,对了l1操作等同于对t1操作
  8         ListNode t2 = l2;
  9         while (t1 != null && t2 != null){
 10             if (t1.val <= t2.val) {
 11                 r1.next = t1;
 12                 t1 = t1.next;
 13             } else {
 14                 r1.next = t2;
 15                 t2 = t2.next;
 16             }
 17             r1 = r1.next;
 18         }
 19         if (t1 != null) {
 20             r1.next = t1;
 21         }
 22         if (t2 != null) {
 23             r1.next = t2;
 24         }
 25         res = res.next;
 26         return res;
 27     }
 28     /**
 29      * @param head
 30      * @param n
 31      * @return ListNode
 32      */
 33     public static ListNode removeNthFromEnd(ListNode head, int n) {
 34         if (n == 0) return head;
 35         ListNode fakeNode = head;
 36         int count = 0;
 37         while (fakeNode != null) {
 38             fakeNode = fakeNode.next;
 39             count++;
 40         }
 41         fakeNode = head;
 42         if (n == count) {
 43             head = head.next;
 44             return head;
 45         } else {
 46             for (int i = 0; i < count; i++) {
 47                 if (i + n + 1== count) {
 48                     System.out.println(fakeNode.val);
 49                     ListNode cut = fakeNode.next.next;
 50                     fakeNode.next = cut;
 51                     count--;
 52                     continue;
 53                 }
 54                 fakeNode = fakeNode.next;
 55             }
 56         }
 57         return head;
 58     }
 59
 60     public static void main(String args[]){
 61         ListNode l1 = new ListNode(0);
 62         ListNode l2 = new ListNode(1);
 63         ListNode p1 = l1;
 64         ListNode p2 = l2;
 65         /*initial the list*/
 66         for (int i = 2; i <= 10; i++) {
 67             if (i%2 == 0) {
 68                 p1.next = new ListNode(i);
 69                 p1 = p1.next;
 70             } else {
 71                 p2.next = new ListNode(i);
 72                 p2 = p2.next;
 73             }
 74         }
 75         p1 = l1;
 76         p2 = l2;
 77         System.out.println("input List l1 and l2");
 78         showData(l1, l2);//after show,l1 and l2 value didn‘t change!
 79         System.out.println("mergeTwoLists(l1, l2) -->");
 80         ListNode res = mergeTwoSortedLists(l1, l2);
 81         /**  test  mergeTwoSortedLists  start ************/
 82         while (res.next != null) {// res is destroyed
 83             System.out.print(res.val + "\t");
 84             res = res.next;
 85         }
 86         System.out.println(res.val);
 87         System.out.println("After merge");
 88         /** End ***********************************/
 89         /** test removeNthFromEnd  start **************/
 90         showData(l1, l2);
 91         // use l2 to test
 92         int n = 1;
 93         l2 = removeNthFromEnd(l2,n);
 94         showData(l1, l2);
 95         /** End ***********************************/
 96     }
 97     /**
 98      * Print the ListNode
 99      * @param l1 ListNode
100      * @param l2 ListNode
101      */
102     public static void showData(ListNode l1, ListNode l2){
103         System.out.println("l1 -->");
104         while (l1.next != null) {
105             System.out.print(l1.val + "\t");
106             l1 = l1.next;
107         }
108         System.out.println(l1.val);
109         System.out.println("l2 -->");
110         while (l2.next != null) {
111             System.out.print(l2.val + "\t");
112             l2 = l2.next;
113         }
114         System.out.println(l2.val);
115         System.out.println("/************************************/");
116     }
117 }
118
119 //Definition for singly-linked list.
120 class ListNode {
121     int val;
122     ListNode next;
123     ListNode(int x) { val = x; }
124 }

输出:

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
/************************************/

时间: 2024-12-25 07:53:26

Merge Two Sorted Lists & Remove Nth Node From End of List的相关文章

#23 Merge k Sorted Lists (N路归并排序)

#23 Merge k Sorted Lists (N路归并排序) 题目地址:#23 题目分类:链表/归并排序/堆排序 题目难度:hard 题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 翻译:合并K个已经排序的链表,返回一个排序好的链表. 思路 暴力法,我们很容易想到:以一个for循环遍历所有的链表,找出最小的,然后将这个节点加入新的链表

[LeetCode]23. Merge k Sorted Lists

23. Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 给定k个排序了的链表,合并k个链表成一个排序链表. 本程序思路: 1)首先得到K个链表的长度和存在len中 2)从K个链表中找到值最小的那个节点,把该节点添加到合并链表中 3)重复len次即可把所有节点添加到合并链表中. 注意事项: 1)K个链表中有的

[LeetCode]148.Merge Two Sorted Lists

[题目] Sort a linked list in O(n log n) time using constant space complexity. [分析] 单链表适合用归并排序,双向链表适合用快速排序.本题可以复用Merge Two Sorted Lists方法 [代码] /********************************* * 日期:2015-01-12 * 作者:SJF0115 * 题目: 148.Merge Two Sorted Lists * 来源:https://

[leetcode]Merge k Sorted Lists @ Python

原题地址:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 题意:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路:归并k个已经排好序的链表.使用堆这一数据结构,首先将每条链表的头节点进入堆中,然后将最小的弹出,并将最小的节点这条链表的下一个节点入堆,依次类推,最终形成的链表就是归

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

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:这题最容易想到的是,(假设有k个链表)链表1.2合并,然后其结果12和3合并,以此类推,最后是123--k-1和k合并.至于两链表合并的过程见merge two sorted lists的分析.复杂度的分析见JustDoIT的博客.算法复杂度:假设每个链表的平均长度是n,则1.2合并,遍历2n个

LeetCode #23 Merge k Sorted Lists (H)

[Problem] Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. [Analysis] 这题一上来,就会想到之前做过的Merge Two Sorted Lists.但是如果单纯地不断将新的list merge进结果,复杂度是非常高的,因为仔细想想就会发现在这个过程中结果里已经merge过的元素需要被重新排序,而这一部分无用功是可以通过使用合理的技

[LintCode] Merge Two Sorted Lists

Merge Two Sorted Lists Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list should be made by splicing together the nodes of the two lists and sorted in ascending order. Example Given 1->3->8->11->1

Merge Two Sorted Lists leetcode java

题目: 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. 题解: 这道题是链表操作题,题解方法很直观. 首先,进行边界条件判断,如果任一一个表是空表,就返回另外一个表. 然后,对于新表选取第一个node,选择两个表表头最小的那个作为新表表头,指针后挪. 然后同时遍历

LeetCode 023 Merge k Sorted Lists

题目要求:Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 分析: 参考网址:http://blog.csdn.net/a83610312/article/details/8554241 思路是:首先将k个链表的第一个节点集合,建堆,然后取出堆顶的节点,链接到结果链表上,然后将该节点的下一个节点入堆,直到所有