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 8863c8c

Browse files
algorithms/src/main/java/ivanmarkovic/algorithms/recursion/ReverseLinkedListIterative.java
1 parent f0c25a7 commit 8863c8c

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ivanmarkovic.algorithms.recursion;
2+
3+
4+
public class ReverseLinkedListIterative {
5+
6+
class ListNode {
7+
int val;
8+
ListNode next;
9+
}
10+
11+
public static ListNode reverse(ListNode head) {
12+
if(head == null || head.next == null)
13+
return head;
14+
ListNode next = head.next;
15+
head.next = null;
16+
while (true) {
17+
ListNode tmp = next.next;
18+
next.next = head;
19+
head = next;
20+
if(tmp == null)
21+
break;
22+
else {
23+
next = tmp;
24+
}
25+
}
26+
return next;
27+
}
28+
29+
}

0 commit comments

Comments
(0)

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