728x90
https://leetcode.com/problems/merge-in-between-linked-lists
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeInBetween(self, list1: ListNode, a: int, b: int, list2: ListNode) -> ListNode:
a_pre_node = None
b_next_node = None
pre_node = None
node = list1
index = 0
while node != None:
if index == a:
a_pre_node = pre_node
if index == b:
b_next_node = node.next
break
pre_node = node
node = node.next
index += 1
a_pre_node.next = list2
pre_node = None
node = list2
while node != None:
pre_node = node
node = node.next
pre_node.next = b_next_node
return list1
728x90
'IT > 코딩테스트' 카테고리의 다른 글
[Leetcode] 234. Palindrome Linked List (0) | 2024.03.22 |
---|---|
[Leetcode] 206. Reverse Linked List (0) | 2024.03.21 |
[Leetcode] 621. Task Scheduler (0) | 2024.03.19 |
[Leetcode] 452. Minimum Number of Arrows to Burst Balloons (0) | 2024.03.19 |
[Leetcode] 57. Insert Interval (0) | 2024.03.17 |