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

Sign up
Appearance settings

Commit 46d93f4

Browse files
algorithms/src/main/java/ivanmarkovic/algorithms/recursion/ReverseLinkedListIterativeStack.java
1 parent 8863c8c commit 46d93f4

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package ivanmarkovic.algorithms.recursion;
2+
3+
import java.util.Stack;
4+
5+
public class ReverseLinkedListIterativeStack {
6+
7+
static class ListNode {
8+
int val;
9+
ListNode next;
10+
}
11+
12+
public static ListNode reverse(ListNode head) {
13+
if(head == null || head.next == null)
14+
return head;
15+
Stack<ListNode> stack = new Stack<>();
16+
while (head != null) {
17+
stack.push(head);
18+
head = head.next;
19+
}
20+
21+
ListNode node = new ListNode(), n = node;
22+
while (!stack.isEmpty()) {
23+
n.next = stack.peek();
24+
n = stack.pop();
25+
}
26+
n.next = null;
27+
return node.next;
28+
29+
}
30+
31+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /