Skip to content

Commit eecc523

Browse files
committed
연결리스트 뒤집기
1 parent a59ede7 commit eecc523

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

ReverseLinkedList/ListNode.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class ListNode {
2+
int val;
3+
ListNode next;
4+
5+
ListNode() {
6+
}
7+
8+
ListNode(int val) {
9+
this.val = val;
10+
}
11+
12+
ListNode(int val, ListNode next) {
13+
this.val = val;
14+
this.next = next;
15+
}
16+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
public class ReverseLinkedList {
3+
4+
public ListNode reverseList(ListNode head) {
5+
ListNode prev = null;
6+
ListNode curr = head;
7+
while (curr != null) {
8+
ListNode nextTemp = curr.next;
9+
curr.next = prev;
10+
prev = curr;
11+
curr = nextTemp;
12+
}
13+
return prev;
14+
}
15+
}
16+
17+
/* USE_recursive
18+
public ListNode reverseList(ListNode head) {
19+
if (head == null || head.next == null) return head;
20+
ListNode p = reverseList(head.next);
21+
head.next.next = head;
22+
head.next = null;
23+
return p;
24+
}
25+
26+
*/

0 commit comments

Comments
 (0)