LeetCode 솔루션 분류
1721. Swapping Nodes in a Linked List
본문
[Leetcode 시즌 3] 2022년 4월 4일 문제입니다.
[Medium] 1721. Swapping Nodes in a Linked List
[Medium] 1721. Swapping Nodes in a Linked List
You are given the head
of a linked list, and an integer k
.
Return the head of the linked list after swapping the values of the kth
node from the beginning and the kth
node from the end (the list is 1-indexed).
Example 1:
Input: head = [1,2,3,4,5], k = 2 Output: [1,4,3,2,5]
Example 2:
Input: head = [7,9,6,6,7,8,3,0,9,5], k = 5 Output: [7,9,6,6,8,7,3,0,9,5]
Constraints:
- The number of nodes in the list is
n
. 1 <= k <= n <= 105
0 <= Node.val <= 100
관련자료
-
링크
댓글 8
핸디맨님의 댓글
- 익명
- 작성일
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def swapNodes(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
swapNode = head
fastNode = head
for _ in range(k-1):
fastNode = fastNode.next
swapNode = swapNode.next
slowNode = head
while fastNode.next is not None:
fastNode = fastNode.next
slowNode = slowNode.next
swapNode.val, slowNode.val= slowNode.val, swapNode.val
return head
Stew님의 댓글
- 익명
- 작성일
class Solution:
def swapNodes(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
elements, current = [], head
while current:
elements.append(current)
current = current.next
if k > len(elements) // 2 + 1:
k = len(elements) - k + 1
elements[k - 1].val, elements[-k].val = elements[-k].val, elements[k - 1].val
return head
bobkim님의 댓글
- 익명
- 작성일
class Solution {
public:
ListNode* swapNodes(ListNode* head, int k) {
int tmp;
ListNode *backNode=NULL;
ListNode *frontNode=NULL;
ListNode *ListNodeTmp=head;
int ListSize=0;
while(ListNodeTmp!=NULL){
ListSize++;
if(ListSize==k){
frontNode=ListNodeTmp;
};
ListNodeTmp=ListNodeTmp->next;
};
ListNodeTmp=head;
for(int i=0;i<ListSize-k;i++){
ListNodeTmp=ListNodeTmp->next;
};
backNode=ListNodeTmp;
tmp=frontNode->val;
frontNode->val=backNode->val;
backNode->val=tmp;
return head;
};
};
CANUS님의 댓글
- 익명
- 작성일
class Solution {
func swapNodes(_ head: ListNode?, _ k: Int) -> ListNode? {
var node: ListNode? = head
var count = 0
var frontNode: ListNode?
var backNode: ListNode?
while true {
count += 1
if count == k {
frontNode = node
}
if let next = node?.next {
node = node?.next
} else {
break
}
}
var back = count - k
node = head
while back > 0 {
back -= 1
node = node?.next
}
backNode = node
guard frontNode != nil else { return nil }
guard backNode != nil else { return nil }
let val = frontNode!.val
frontNode!.val = backNode!.val
backNode!.val = val
return head
}
}
9dea0936님의 댓글
- 익명
- 작성일
class Solution:
def swapNodes(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
p1, p2, idx, temp, idx2 = 0, 0, 0, head, 0
while temp:
if idx + 1 == k:
p1 = temp
idx += 1
temp = temp.next
temp = head
while temp:
if idx - (idx2) == k:
p2 = temp
idx2 += 1
temp = temp.next
p1.val, p2.val = p2.val, p1.val
return head
JasonMin님의 댓글
- 익명
- 작성일
package main
import "fmt"
type Node struct {
val int
next *Node
}
type List struct {
head *Node
len int
}
func (l *List) Insert(value int) {
node := &Node{val: value, next: nil}
if l.head == nil {
l.head = node
l.len += 1
} else {
x := l.head
for x.next != nil {
x = x.next
}
x.next = node
l.len += 1
}
}
func Show(l *List) {
e := l.head
for i := 0; i < l.len; i++ {
fmt.Println(e.val)
e = e.next
}
}
func (l *List) Swap(idx int) {
gIx := idx - 1
tIx := l.len - gIx - 1
e := l.head
node := &Node{}
for i := 0; i < l.len; i++ {
if gIx == i {
node = e
}
if tIx == i {
e.val, node.val = node.val, e.val
}
e = e.next
}
}
func main() {
l := List{}
for i := 1; i <= 5; i++ {
l.Insert(i)
}
l.Swap(2)
Show(&l)
}