单链表的创建、打印、删除和反转

57 篇文章 1 订阅
52 篇文章 2 订阅

    单链表是一种非常重要的数据结构,它的优点是采用指针方式来增减结点,非常方便(它只用改变指针的指向即可,不需要移动结点);缺点是不能进行随机访问,只能顺着指针的方向进行顺序访问。
    下面,介绍单例表的创建、打印和反转。
    1、定义单链表

struct ListNode {
	int val;
	ListNode *next;
	ListNode(int x) : val(x), next(NULL) {}
};

    2、创建单链表

ListNode* createLinkedList(int arr[], int n) {
	if (n == 0) {
		return NULL; 
	}

	ListNode* head = new ListNode(arr[0]);

	ListNode* curNode = head;
	for (int i = 1; i < n; i++) {
		curNode->next = new ListNode(arr[i]);
		curNode = curNode->next;
	}

	return head;
}

    3、打印单链表

void printLinkedList(ListNode* head) {
	ListNode* curNode = head;
	while (curNode != NULL) {
		cout << curNode->val << "->";
		curNode = curNode->next;
	}
	cout << "NULL" << endl;
}

    4、反转单链表
    //该方法是采用头插法,来反转单链表

class Solution {
public:
	//不带头结点,反转单链表(采用头插法实现)
	ListNode* reverseList(ListNode* head) {
		ListNode* pre = NULL, *next=NULL;
		ListNode* cur = head;
		while (cur != NULL) {
			next = cur->next;
			cur->next = pre;
			pre = cur;
			cur = next;
		}
		return pre;
	}
};

    5、删除单链表

void deleteLinkdList(ListNode* head) {
	ListNode* curNode = head;
	while (curNode != NULL) {
		ListNode* delNode = curNode;
		curNode = curNode->next;
		delete delNode;
		delNode = NULL;
	}
}

   6、 完整代码如下:
    //singleLink.cpp

#include <iostream>
#include <vector>
#include <cassert>

using namespace std;

struct ListNode {
	int val;
	ListNode *next;
	ListNode(int x) : val(x), next(NULL) {}
};

ListNode* createLinkedList(int arr[], int n) {
	if (n == 0) {
		return NULL; 
	}

	ListNode* head = new ListNode(arr[0]);

	ListNode* curNode = head;
	for (int i = 1; i < n; i++) {
		curNode->next = new ListNode(arr[i]);
		curNode = curNode->next;
	}

	return head;
}

void printLinkedList(ListNode* head) {
	ListNode* curNode = head;
	while (curNode != NULL) {
		cout << curNode->val << "->";
		curNode = curNode->next;
	}
	cout << "NULL" << endl;
}

void deleteLinkdList(ListNode* head) {
	ListNode* curNode = head;
	while (curNode != NULL) {
		ListNode* delNode = curNode;
		curNode = curNode->next;
		delete delNode;
		delNode = NULL;
	}
}

class Solution {
public:
	//不带头结点,反转单链表(采用头插法实现)
	ListNode* reverseList(ListNode* head) {
		ListNode* pre = NULL, *next=NULL;
		ListNode* cur = head;
		while (cur != NULL) {
			next = cur->next;
			cur->next = pre;
			pre = cur;
			cur = next;
		}
		return pre;
	}
};

int main() {
	int arr[] = { 1, 2, 3, 4, 5 };
	int n = sizeof(arr) / sizeof(int);
	ListNode* head = createLinkedList(arr, n);
	printLinkedList(head);

	ListNode* head2 = Solution().reverseList(head);
	printLinkedList(head2);

	deleteLinkdList(head2);

	system("pause");
	return 0;
}

    7、效果如下:

图(1) 反转单链表
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sanqima

一键三连,多多益善

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值