You are given the heads of two sorted linked lists list1 and list2.
Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.
Return the head of the merged linked list.
Example 1:
Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]
Example 2:
Input: list1 = [], list2 = []
Output: []
Example 3:
Input: list1 = [], list2 = [0]
Output: [0]
Constraints:
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
merge := &ListNode{}
head := merge
for list1 != nil && list2 != nil {
if list1.Val <= list2.Val {
merge.Next = list1
merge = merge.Next
list1 = list1.Next
} else {
merge.Next = list2
merge = merge.Next
list2 = list2.Next
}
}
if list1 != nil {
merge.Next = list1
} else {
merge.Next = list2
}
return head.Next
}
https://go.dev/play/p/LWaEztxvxjj
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
dummyHead := ListNode{}
p := &dummyHead
for list1 != nil && list2 != nil {
if list1.Val < list2.Val {
p.Next = list1
list1 = list1.Next
} else {
p.Next = list2
list2 = list2.Next
}
p = p.Next
}
if list1 != nil { p.Next = list1 }
if list2 != nil { p.Next = list2 }
return dummyHead.Next
}
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
if list1 == nil {return list2}
if list2 == nil {return list1}
if list1.Val <= list2.Val {
list1.Next = mergeTwoLists(list1.Next, list2)
return list1
} else {
list2.Next = mergeTwoLists(list1, list2.Next)
return list2
}
}
func addToResult(list **ListNode, newList **ListNode) {
(*newList).Next = *list
*list = (*list).Next
*newList = (*newList).Next
(*newList).Next = nil
}
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
var result *ListNode
if list1 == nil {
return list2
}
if list2 == nil {
return list1
}
if list1.Val <= list2.Val {
result = list1
list1 = list1.Next
} else {
result = list2
list2 = list2.Next
}
newList := result
LOOP:
for list1 != nil || list2 != nil {
if list2 == nil {
newList.Next = list1
break LOOP
}
if list1 == nil {
newList.Next = list2
break LOOP
}
if list1.Val <= list2.Val {
newList.Next = list1
newList = list1
list1 = list1.Next
} else {
newList.Next = list2
newList = list2
list2 = list2.Next
}
}
return result
}
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
dump := &ListNode{}
temp := dump
for list1 != nil && list2 != nil {
if list1.Val > list2.Val{
temp.Next = list2
list2 = list2.Next
}else {
temp.Next = list1
list1 = list1.Next
}
temp = temp.Next
}
if list1 != nil {
temp.Next = list1
}
if list2 != nil {
temp.Next = list2
}
return dump.Next
}
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
root := &ListNode{}
curr := root
for list1 != nil || list2 != nil {
curr.Next = &ListNode{}
curr = curr.Next
if list1 != nil && list2 != nil {
if list1.Val < list2.Val {
curr.Val = list1.Val
list1 = list1.Next
} else {
curr.Val = list2.Val
list2 = list2.Next
}
} else if list1 != nil {
curr.Val = list1.Val
list1 = list1.Next
} else {
curr.Val = list2.Val
list2 = list2.Next
}
}
return root.Next
}
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
dummyHead := &ListNode{}
currentNode := dummyHead
for list1 != nil && list2 != nil {
if list1.Val < list2.Val {
currentNode.Next = list1
list1 = list1.Next
} else {
currentNode.Next = list2
list2 = list2.Next
}
currentNode = currentNode.Next
}
if list1 != nil {
currentNode.Next = list1
}
if list2 != nil {
currentNode.Next = list2
}
return dummyHead.Next
}
[Go] Linked List - Add Two Numbers (0) | 2022.05.14 |
---|---|
[Go] Linked list - Doubly Linked List from java to GO (0) | 2022.05.09 |
[Go] Linked List - Palindrome Linked List (0) | 2022.05.09 |
[Go] Linked List - Odd Even Linked List (0) | 2022.05.06 |
[Go] Linked List - Remove Linked List Elements (0) | 2022.05.05 |
댓글 영역