链表算法题

链表相关技巧

链表算法题技巧一般有增加头结点、使用快慢指针、递归的先序后序使用。本文主要为递归相关笔记。

链表翻转

leetcode206:反转链表

1.先序递归双指针参数(增加NULL头结点,方便反转操作),每次更改一个方向,可直接转换成循环,书写正确率高,优先使用

class Solution{
public:
	ListNode* reverseList(ListNode* head) {
		if(!head) return NULL; //leetcode特殊样例处理
		ListNode* pre = NULL;
		return reverse(pre,head); //先序递归反转
	}
	ListNode* reverse(ListNode* pre,ListNode* cur){
		if(cur==NULL) return pre;
		ListNode* tmp = cur->next;//先序
		cur->next = pre;//先序
		return reverse(cur,tmp);//递归
	}
};
// 1->2->3->4->5->null
// null  1->2->3->4->5->null 增加NULL头结点
// null<-1<-2<-3<-4<-5

2.后序递归:单指针参数,对递归思想的理解要求较高,书写易错
思想:长n链表反转问题可转化为长n-1链表反转,递归到长1后,回溯至长n

class Solution{
public:
	ListNode* reverseList(ListNode* head) {
		if(!head) return NULL;//leetcode样例空链表
		//后序递归反转
		if(!head->next) return head;//单个结点无需反转
		ListNode* last = reverse(head->next);//递归
		head->next->next = head;//后序
		head->next = NULL;//后序
		return last;
	}
};
// 1->2->3->4->5->null
// 5->null
// 5->4->null
// ...
// 5->4->3->2->1->null

leetcode25:K个一组翻转链表

1.K个一组先序递归

class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        if(head==NULL) return NULL;
        ListNode* a=head, *b = head;
        for(int i=0;i<k;i++){
            if(b==NULL) return head;
            b= b->next;
        }
        ListNode* newhead = reverse(a,b);
        a->next = reverseKGroup(b,k);
        return newhead;
    }
    ListNode* reverse(ListNode* head,ListNode* b) {
        if(head->next==b) return head;
        ListNode* last = reverse(head->next,b);
        head->next->next = head;
        head->next = NULL;
        return last;
    }
};

leetcode92:反转链表II

1.后序递归(先序递归较为直观,省去)

class Solution {
public:
    ListNode* successor = NULL;
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        if(m==1){
        	return reverseN(head,n);
        }
        head.next = reverseBetween(head->next,m-1,n-1);
        return head;
    }
    ListNode* reverse(ListNode* head,int n){
    	if(n==1){
    		successor = head->next;
    		return head;
    	}
    	ListNode* last = reverse(head->next,n-1);
    	head->next->next = head;
    	head->next = successor;
    	return last;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值