• 문제 링크
Swap Nodes in Pairs - LeetCode
Can you solve this real interview question? Swap Nodes in Pairs - Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be chan
leetcode.com
• 풀이 코드
public class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(-1), cur = dummy;
cur.next = head;
while (cur.next != null && cur.next.next != null) {
ListNode a = cur.next, b = cur.next.next;
a.next = b.next;
b.next = a;
cur.next = b;
cur = cur.next.next;
}
return dummy.next;
}
}'Problem Solving > LeetCode' 카테고리의 다른 글
| [LeetCode] 58. Length of Last Word - Java (0) | 2026.05.19 |
|---|---|
| [LeetCode] 35. Search Insert Position - Java (0) | 2026.05.18 |
| [LeetCode] 28. Find the Index of the First Occurrence in a String - Java (0) | 2026.05.16 |
| [LeetCode] 27. Remove Element - Java (0) | 2026.05.15 |
| [LeetCode] 26. Remove Duplicates from Sorted Array - Java (0) | 2026.05.14 |
댓글