剑指Offer 24. 反转链表
题目要求反转整个链表,假设reverse(head)方法可以反转以head为头节点的链表,并返回反转后的头节点(即原来的尾),那么reverse()方法题里面存在一个规模更小的子问题,可以递归处理整个过程
reverse(head)=>{
ListNode last = reverse(head.next); // 先将head节点后面的链表反转
head.next.next = head; // 再让后面已经反转好的链表的最后的节点指向自己
head.next = null;
}
处理一下递归出口:{head.next == null;return head;}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
// 这个是特殊情况
if (head == null) return null;
// 这个是递归出口
if (head.next == null) return head;
ListNode last = reverseList(head.next);
head.next.next = head;
head.next = null;
return last;
}
}
LeetCode 92. 反转链表II
此题要求将链表一个区间进行反转,先解决一个与上一题类似的问题:【反转链表的前n个节点】;与上一题不同的就是只需要递归n层,还需要存下不需要反转的后面的链表的头结点,前面反转完,让前面反转完的尾结点(即原来的头结点)的next指向不需要反转的头结点即可
即:
ListNode _last = null;
public ListNode reverseN(ListNode head,int n){
if (n == 1){
_last = head.next;
return head;
}
ListNode last = reverseN(head.next,n - 1);
head.next.next = head;
head.next = _last;
return last;
}
}
处理完reverseN问题,再回到本题,当处理区间刚好从1开始时就等于reverseN问题了,那么很简单,只需要特殊处理left == 1的情况,一直使头结点往后(类似于扔点头结点,直到left == 1)
所有完整代码
/**
* 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; }
* }
*/
class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
if(left == 1){
return reverseN(head,right);
}
head.next = reverseBetween(head.next,left - 1,right - 1);
return head;
}
ListNode _last = null;
public ListNode reverseN(ListNode head,int n){
if (n == 1){
_last = head.next;
return head;
}
ListNode last = reverseN(head.next,n - 1);
head.next.next = head;
head.next = _last;
return last;
}
}
LeetCode 25. K 个一组翻转链表
本题要求每k个区间就反转一次链表,当后面不够k就不反转,同样是可以用reverseN来解决,不过需要判断以head为头的链表,后面够不够k个,够就反转,不够就不反,[注意前面反转完的链表的尾结点的next应该指向后面的头,而反转后的尾结点就是原来的head]
/**
* 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; }
* }
*/
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null) return head;
ListNode b = head;
for(int i = 0;i < k;i++){
if (b == null) return head;
b = b.next;
}
ListNode newNode = reverseN(head,k);
head.next = reverseKGroup(b,k);
return newNode;
}
ListNode _last = null;
public ListNode reverseN(ListNode head,int n){
if (n == 1){
_last = head.next;
return head;
}
ListNode last = reverseN(head.next,n - 1);
head.next.next = head;
head.next = _last;
return last;
}
}
LeetCode 234. 回文链表
本来此题可以用链表的前序遍历和后序遍历比较得出结果,但是需要空间O(n),利用reverseList使空间降为O(1),先用快慢指针找到链表的中点,然后反转后面的链表,再比较每个节点,注意这样会改变输入链表的结构,如果需要维护这个结构要复原,可能因为用递归的方法实现reverseList,在LeetCode上时间没那么快,用迭代方式实现应该更优
* 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; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
if (head == null) return true;
ListNode slow,fast;
slow = fast = head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
if(fast != null) slow = slow.next;
ListNode p = reverseList(slow);
while(p != null){
if(p.val != head.val){
return false;
}
p = p.next;
head = head.next;
}
reverseList(p);
return true;
}
public ListNode reverseList(ListNode head) {
// 两个特殊情况
if (head == null) return null;
if (head.next == null) return head;
ListNode last = reverseList(head.next);
head.next.next = head;
head.next = null;
return last;
}
}