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

Commitab11f6b

Browse files
reverse linked list iterative
1 parent852fba6 commitab11f6b

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

‎EASY/src/easy/ReverseLinkedList.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
packageeasy;
2+
3+
importutils.CommonUtils;
4+
importclasses.ListNode;
5+
6+
publicclassReverseLinkedList {
7+
//there's no reversion if the list is null or it contains only one node, so at a minimum, there should be 2 nodes
8+
publicListNodereverseList(ListNodehead) {
9+
ListNodenewHead =null;
10+
while(head !=null){
11+
ListNodenext =head.next;
12+
head.next =newHead;
13+
newHead =head;
14+
head =next;
15+
}
16+
returnnewHead;
17+
}
18+
19+
publicstaticvoidmain(String...strings){
20+
ReverseLinkedListtest =newReverseLinkedList();
21+
ListNodehead =newListNode(1);
22+
head.next =newListNode(2);
23+
head.next.next =newListNode(3);
24+
head.next.next.next =newListNode(4);
25+
head.next.next.next.next =newListNode(5);
26+
CommonUtils.printList(head);
27+
ListNoderesult =test.reverseList(head);
28+
CommonUtils.printList(result);
29+
}
30+
31+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp