首页 > 其他分享 >LeetCode 1669. Merge In Between Linked Lists

LeetCode 1669. Merge In Between Linked Lists

时间:2024-05-16 11:53:51浏览次数:12  
标签:ListNode cur val list1 Lists next Merge list2 LeetCode

原题链接在这里:https://leetcode.com/problems/merge-in-between-linked-lists/description/

题目:

You are given two linked lists: list1 and list2 of sizes n and m respectively.

Remove list1's nodes from the ath node to the bth node, and put list2 in their place.

The blue edges and nodes in the following figure indicate the result:

Build the result list and return its head. 

Example 1:

Input: list1 = [10,1,13,6,9,5], a = 3, b = 4, list2 = [1000000,1000001,1000002]
Output: [10,1,13,1000000,1000001,1000002,5]
Explanation: We remove the nodes 3 and 4 and put the entire list2 in their place. The blue edges and nodes in the above figure indicate the result.

Example 2:

Input: list1 = [0,1,2,3,4,5,6], a = 2, b = 5, list2 = [1000000,1000001,1000002,1000003,1000004]
Output: [0,1,1000000,1000001,1000002,1000003,1000004,6]
Explanation: The blue edges and nodes in the above figure indicate the result.

Constraints:

  • 3 <= list1.length <= 104
  • 1 <= a <= b < list1.length - 1
  • 1 <= list2.length <= 104

题解:

when index hit a - 1, mark1, when hit b - 1, mark2.

mark1.next = list2. 

Move list2 to the tail and point to mark2.next.

Time Compelxity; O(b + n). n = list2 length.

Space: O(1).

AC Java:

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode() {}
 7  *     ListNode(int val) { this.val = val; }
 8  *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 9  * }
10  */
11 class Solution {
12     public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
13         ListNode cur = list1;
14         ListNode mark1 = null;
15         for(int i = 0; i < b; i++){
16             if(i == a - 1){
17                 mark1 = cur;
18             }
19 
20             cur = cur.next;
21         }
22 
23         mark1.next = list2;
24         while(list2.next != null){
25             list2 = list2.next;
26         }
27 
28         list2.next = cur.next;
29         cur.next = null;
30         return list1;
31     }
32 }

 

标签:ListNode,cur,val,list1,Lists,next,Merge,list2,LeetCode
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18195678

相关文章

  • LeetCode 2595. Number of Even and Odd Bits
    原题链接在这里:https://leetcode.com/problems/number-of-even-and-odd-bits/description/题目:Youaregivena positive integer n.Let even denotethenumberofevenindicesinthebinaryrepresentationof n (0-indexed)withvalue 1.Let odd denotethen......
  • 39. 组合总和(leetcode)
    https://leetcode.cn/problems/combination-sum/description/两种搜索思路一种是选或不选,搜索树是一颗二叉树另一种是选哪个数,是一个n叉树二叉classSolution{List<List<Integer>>res=newArrayList<>();inttarget;int[]nums;List<Integer>......
  • 108. 将有序数组转换为二叉搜索树(leetcode)
    https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/description/要点是分割左右区间,并且分割时需要注意left和right相加可能会超过int,但是本题不需要classSolution{publicTreeNodesortedArrayToBST(int[]nums){//有返回值写法......
  • LeetCode 1992. Find All Groups of Farmland
    原题链接在这里:https://leetcode.com/problems/find-all-groups-of-farmland/description/题目:Youaregivena 0-indexed mxn binarymatrix land wherea 0 representsahectareofforestedlandanda 1 representsahectareoffarmland.Tokeepthelandor......
  • [LeetCode] Find the Minimum Cost Array Permutation
    Youaregivenanarray nums whichisa permutation of [0,1,2,...,n-1].The score ofanypermutationof [0,1,2,...,n-1] named perm isdefinedas:score(perm)=|perm[0]-nums[perm[1]]|+|perm[1]-nums[perm[2]]|+...+|perm[n-1]-......
  • array_merge和+的区别
    array_merge()array_merge()将一个或多个数组合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果数组1.字符串键后面的值会覆盖前面的一个值。2.数字键,后面的值将不会覆盖原来的值而是附加到后面(数字键会重新分配,总是变成重零开始)3.如果只给了一个数组并该数组是数......
  • LeetCode 2956. Find Common Elements Between Two Arrays
    原题链接在这里:https://leetcode.com/problems/find-common-elements-between-two-arrays/description/题目:Youaregiventwo 0-indexed integerarrays nums1 and nums2 ofsizes n and m,respectively.Considercalculatingthefollowingvalues:Thenumberof......
  • LeetCode 1915. Number of Wonderful Substrings
    原题链接在这里:https://leetcode.com/problems/number-of-wonderful-substrings/description/题目:A wonderful stringisastringwhere atmostone letterappearsan odd numberoftimes.Forexample, "ccjjc" and "abab" arewonderful,but "ab&......
  • 【LeetCode 875】爱吃香蕉的珂珂
    题目描述原题链接:LeetCode.875爱吃香蕉的珂珂解题思路如果当前堆剩余香蕉数量小于每小时吃的数量,吃完当前堆就会休息不会去吃下一堆的香蕉,所以吃完一堆所需时间就是堆的香蕉数量除以速度的向上取整值:\(\lceil{piles[i]/speed}\rceil\);首先确定答案所处的范围,速度最小......
  • [LeetCode] 1584.连接所有点的最小费用 Kruskal And Prim 算法 Java 并查集
    Problem:1584.连接所有点的最小费用目录Kruskal算法复杂度CodePrim算法复杂度CodeKruskal算法复杂度时间复杂度:添加时间复杂度,示例:$O(mlog(m))$空间复杂度:添加空间复杂度,示例:$O(n)$CodeclassSolution{publicintminCostConnectPoints(int[][]po......