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

Commitc4b6368

Browse files
Create 24 Swap Nodes in Pairs.java
1 parent5be3c73 commitc4b6368

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

‎java/24 Swap Nodes in Pairs.java‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//Iterative version
2+
classSolution {
3+
publicListNodeswapPairs(ListNodehead) {
4+
if (head ==null ||head.next ==null)returnhead;
5+
ListNodedummy =newListNode(0);
6+
dummy.next =head;
7+
ListNodetemp =dummy;
8+
while (temp.next!=null &&temp.next.next!=null) {
9+
ListNodefirst =temp.next;
10+
ListNodesecond =temp.next.next;
11+
temp.next =second;
12+
first.next =second.next;
13+
second.next =first;
14+
temp =first;
15+
}
16+
returndummy.next;
17+
18+
}
19+
}
20+
21+
//Recursive version
22+
classSolution {
23+
publicListNodeswapPairs(ListNodehead) {
24+
if (head ==null ||head.next ==null)
25+
returnhead;
26+
ListNodep =head.next;
27+
head.next =swapPairs(head.next.next);
28+
p.next =head;
29+
returnp;
30+
}
31+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp