File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments