Leedcode00---链表入门

题目在下面写出来了

#include<iostream>
using namespace std;

class ListNode00
{
public:
	int val;
	ListNode00* next;
};

void test00()
{
	ListNode00  a;
	ListNode00  b;
	ListNode00  c;
	ListNode00  d;
	ListNode00  e;
	a.val = 10;
	b.val = 20;
	c.val = 30;
	d.val = 40;
	e.val = 50;
	a.next = &b;
	b.next = &c;
	c.next = &d;
	d.next = &e;
	e.next = NULL;

	ListNode00* head = &a;
	while (head)
	{
		cout << head->val << " ";
		head = head->next;
	}
}

//1链表逆序  -- 直接逆序
class ListNode01
{
public:
	int val;
	ListNode01* next;
	ListNode01(int x) :val(x), next(NULL) {}
};
class Solution1
{
public:
	ListNode01* reverseList(ListNode01* head)
	{
		ListNode01* new_head = NULL;		//指向新链表头节点的指针
		while (head)
		{
			ListNode01* next = head->next;	//备份head->next;
			head->next = new_head;			//更新head->next
			new_head = head;				//移动new_head
			head = next;					//遍历链表
		}
		return new_head;				//返回新链表的头节点
	}
};

void test01()
{
	ListNode01  a(10);
	ListNode01  b(20);
	ListNode01  c(30);
	ListNode01  d(40);
	ListNode01  e(50);
	a.next = &b;
	b.next = &c;
	c.next = &d;
	d.next = &e;
	Solution1 solve;
	ListNode01* head = &a;
	cout << "before reverse: " << endl;
	while (head)
	{
		cout << head->val<<"  ";
		head = head->next;
	}
	cout << endl;
	head = solve.reverseList(&a);
	cout << "after reverse: " << endl;
	while (head)
	{
		cout << head->val<<"  ";
		head = head->next;
	}
	cout << endl;
}

//从链表的m到n的位置逆序,上面的升级版1<=m<n<=链表长度
class ListNode02
{
public:
	int val;
	ListNode02* next;
	ListNode02(int x) :val(x), next(NULL) {}
};
class Solution2
{
public:
	ListNode02* reverseList(ListNode02* head,int m,int n)
	{
		int change_len = n - m + 1;
		ListNode02* pre_head = NULL;
		ListNode02* result = head;
		while (head && --m)				//将head向前移动m-1个位置
		{
			pre_head = head;
			head = head->next;
		}
		ListNode02* modify_list_tail = head;
		ListNode02* new_head = NULL;
		while (head && change_len)		//逆置change_len个节点
		{
			ListNode02* next = head->next;
			head->next = new_head;
			new_head = head;
			head = next;
			change_len--;
		}
		modify_list_tail->next = head;
		if (pre_head)
		{
			pre_head->next = new_head;
		}
		else
		{
			result = new_head;
		}
		return result;
	}
};

void test02()
{
	ListNode02  a(10);
	ListNode02  b(20);
	ListNode02  c(30);
	ListNode02  d(40);
	ListNode02  e(50);
	a.next = &b;
	b.next = &c;
	c.next = &d;
	d.next = &e;
	Solution2 solve;
	ListNode02* head = &a;
	cout << "before reverse: " << endl;
	while (head)
	{
		cout << head->val << "  ";
		head = head->next;
	}
	cout << endl;
	head = solve.reverseList(&a,2,4);
	cout << "after reverse: " << endl;
	while (head)
	{
		cout << head->val << "  ";
		head = head->next;
	}
	cout << endl;
}

int main()
{
	//test00();
	test02();

	system("pause");
	return 0;
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值