We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent fb07581 commit 33caec6Copy full SHA for 33caec6
206. Reverse Linked List
@@ -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
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