Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
29 lines (27 loc) · 968 Bytes

File metadata and controls

29 lines (27 loc) · 968 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package ch08;
import datatype.ListNode;
public class P17_2 {
public ListNode swapPairs(ListNode head) {
// 값을 계산할 임시 노드 선언
ListNode node = new ListNode(0);
// 임시 노드를 첫 번째 노드로 선언
ListNode root = node;
// 다음 노드는 첫 번째 노드로
node.next = head;
// 다음 노드와 다음 다음 노드가 있으면 반복
while (node.next != null && node.next.next != null) {
// a는 다음 노드
ListNode a = node.next;
// b는 다음 다음 노드
ListNode b = node.next.next;
// 위치 변경
a.next = b.next;
node.next = b;
node.next.next = a;
// 두 칸 앞으로 이동
node = node.next.next;
}
// 첫 번째 노드는 임시 노드이므로 그다음부터 결과로 리턴
return root.next;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.