File tree 2 files changed +42
-0
lines changed 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class ListNode {
2
+ int val ;
3
+ ListNode next ;
4
+
5
+ ListNode () {
6
+ }
7
+
8
+ ListNode (int val ) {
9
+ this .val = val ;
10
+ }
11
+
12
+ ListNode (int val , ListNode next ) {
13
+ this .val = val ;
14
+ this .next = next ;
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+
2
+ public class ReverseLinkedList {
3
+
4
+ public ListNode reverseList (ListNode head ) {
5
+ ListNode prev = null ;
6
+ ListNode curr = head ;
7
+ while (curr != null ) {
8
+ ListNode nextTemp = curr .next ;
9
+ curr .next = prev ;
10
+ prev = curr ;
11
+ curr = nextTemp ;
12
+ }
13
+ return prev ;
14
+ }
15
+ }
16
+
17
+ /* USE_recursive
18
+ public ListNode reverseList(ListNode head) {
19
+ if (head == null || head.next == null) return head;
20
+ ListNode p = reverseList(head.next);
21
+ head.next.next = head;
22
+ head.next = null;
23
+ return p;
24
+ }
25
+
26
+ */
You can’t perform that action at this time.
0 commit comments