Skip to content

Commit 33caec6

Browse files
authored
Create 206. Reverse Linked List
1 parent fb07581 commit 33caec6

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

206. Reverse Linked List

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
if(head==null || head.next==null) return head;
15+
ListNode newHead = reverseList(head.next);
16+
head.next.next = head;
17+
head.next = null;
18+
return newHead;
19+
}
20+
}
21+
22+
23+
class Solution {
24+
public ListNode reverseList(ListNode head) {
25+
ListNode prev=null,curr=head;
26+
while(curr!=null){
27+
ListNode next = curr.next;
28+
curr.next=prev;
29+
prev=curr;
30+
curr=next;
31+
}
32+
return prev;
33+
}
34+
}

0 commit comments

Comments
 (0)