Skip to content

Commit 2599e8c

Browse files
committed
Added Reverse Linked List
1 parent 4010ccc commit 2599e8c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Reverse_Linked_List.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
12+
class Solution {
13+
public ListNode reverseList(ListNode head) {
14+
ListNode newHead = null;
15+
while (head != null) {
16+
ListNode next = head.next;
17+
head.next = newHead;
18+
newHead = head;
19+
head = next;
20+
}
21+
return newHead;
22+
}
23+
}

0 commit comments

Comments
 (0)