-
Notifications
You must be signed in to change notification settings - Fork 0
/
D_reverseList.java
63 lines (57 loc) · 1.4 KB
/
D_reverseList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.example.mvcdemo.test.b链表;
/**
*给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
*
* 示例 1:
* 输入:head = [1,2,3,4,5]
* 输出:[5,4,3,2,1]
*
* 难度:easy
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/reverse-linked-list
*/
/**
* 思路:
* 1.递归,注意中断条件是head.next == null
* 2.三指针
*/
public class D_reverseList {
// Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
public ListNode reverseList(ListNode head) {
if(head==null || head.next == null){
return head;
}
ListNode res = reverseList(head.next);
ListNode next = head.next;
next.next = head;
head.next = null;
return res;
}
public ListNode reverseList2(ListNode head){
if(head==null || head.next == null){
return head;
}
ListNode pre = null;
ListNode next = head;
while(head!=null){
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
}