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
30 lines (28 loc) · 1020 Bytes

File metadata and controls

30 lines (28 loc) · 1020 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
30
package ch08;
import datatype.ListNode;
public class P19_1 {
public ListNode reverseBetween(ListNode head, int left, int right) {
// 예외 처리
if (head == null)
return null;
// 임시 노드 선언
ListNode root = new ListNode(0);
// 임시 노드 다음으로 노드 시작
root.next = head;
// 임시 노드부터 시작해 변경 필요한 위치 앞으로 이동
ListNode start = root;
for (int i = 0; i < left - 1; i++)
start = start.next;
// 변경이 필요한 마지막 위치 선언
ListNode end = start.next;
// right - left만큼 위치 변경 진행
for (int i = 0; i < right - left; i++) {
ListNode tmp = start.next;
start.next = end.next;
end.next = end.next.next;
start.next.next = tmp;
}
// 첫 번째 노드는 임시 노드이므로 그다음부터 결과로 리턴
return root.next;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.