Skip to content

Commit 3ca5871

Browse files
committed
solve reverse linked list
1 parent a98baee commit 3ca5871

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

reverse-linked-list/sora0319.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode reverseList(ListNode head) {
13+
if(head == null) return null;
14+
15+
ListNode backward = null;
16+
ListNode forward = head;
17+
18+
while(forward != null){
19+
forward = head.next;
20+
head.next = backward;
21+
backward = head;
22+
if(forward != null) head = forward;
23+
}
24+
25+
return head;
26+
}
27+
}
28+

0 commit comments

Comments
 (0)