Movatterモバイル変換


[0]ホーム

URL:


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

Commit27c3fa9

Browse files
Merge pull request#315 from SharmaTushar1/patch-12
Create 206. Reverse Linked List.java
2 parentsd7c1b5a +8fd4d54 commit27c3fa9

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

‎java/206. Reverse Linked List.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//Use three pointers and so you can change the next of the mid to the first one without losing the track of the original left.
2+
//Iterative version
3+
classSolution {
4+
publicListNodereverseList(ListNodehead) {
5+
ListNodep =null;
6+
ListNodeq =null;
7+
ListNoder =head;
8+
while (r!=null) {
9+
p =q;
10+
q =r;
11+
r =r.next;
12+
q.next =p;
13+
}
14+
returnq;
15+
}
16+
}
17+
18+
//Recursive version
19+
classSolution {
20+
publicListNodereverseList(ListNodehead) {
21+
returnrev(head,null);
22+
}
23+
24+
publicListNoderev(ListNodenode,ListNodepre) {
25+
if (node ==null)
26+
returnpre;
27+
ListNodetemp =node.next;
28+
node.next =pre;
29+
returnrev(temp,node);
30+
}
31+
32+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp