顺序表链表OJ题

1、单链表的增删查改

#include "slist.h"

SListNode*BuySListNode(SLTDateType x)

{

	SListNode*node = (SListNode*)malloc(sizeof(SListNode));

	node->data = x;

	node->next = NULL;

	return node;

}

void SListPrint(SListNode*plist)

{

	SListNode*cur = plist;

	while (cur)

	{

		printf("%d->", cur->data);

		cur = cur->next;

	}

	printf("NULL\n");

}

void SListPushBack(SListNode**pplist, SLTDateType x)

{

	SListNode*newnode = BuySlistNode(x);

	if (*pplist == NULL)

	{

		*pplist == newnode;

	}

	else

	{

		SListNode*tail = *pplist;

		while (tail->next != NULL)

		{

			tail = tail->next;

		}

		tail->next = newnode;

	}

}



void SListBack(SListNode**pplist)

{

	SListNode*prev = NULL;

	SListNode*tail = *pplist;

	if (tail == NULL || tail->next == NULL)

	{

		free(tail);

		*pplist = NULL;

	}

	else

	{

		while (tail->next)

		{

			prev = tail;

			tail = tail -> next;

		}

		free(tail);

		tail = NULL;

		prev->next = NULL;

	}

}

void SListPushFront(SListNode**pplist, SLTDateType x)

{

	assert(pplist);

	SListNode*newode = BuySListNode(x);

	if (*pplist == NULL)

	{

		*pplist = newnode;

	}

	else

	{

		newnode->next = *pplist;

		*pplist = newnode;

	}

}

void SListPopFront(SListNode**pplist)

{

	SListNode*first = *pplist;

	if (first == NULL)

	{

		return;

	}

	else if (first->next == NULL)

	{

		free(first);

		*pplist = NULL;

	}

	else

	{

		SListNode*next = first->next;

		free(first);

		*pplist = next;

	}



}



SListNode*SListFind(SListNode*pplist, SLTDateType x)

{

	SListNode*cur = plist;

	while (cur)

	{

		if (cur->data == x)

			return cur;

		cur = cur->next;

	}

	return NULL;

}

void SListInsertAfter(SListNode*pos, SLTDateType x)

{

	assert(pos);

	SListNode*next = pos->next;

	SListNode*newnode = BuySListNode(x);

	pos->next = newnode;

	newnode->next = next;

}

void SListEraseAfter(SListNode*pos)

{

	assert(pos);

	SListNode*next = pos->next;

	if (next != NULL)

	{

		SListNode*nextnext = next->next;

		free(next);

		pos->next = nextnext;

	}

}

2、给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。
C语言:

struct ListNode*removeElements(struct ListNode*head, int val)
{
	if (head == NULL)
	{
		return head;
	}
	head->next = removeElements(head->next, val);
	{
		return head->val == val ? head->next : head;
	}
}

c++:

class Solution
{
public:
	ListNode*removeElements(ListNode*head, int val)
	{
		if (head == nullptr)
		{
			return head;
		}
		head->next = removeElements(head->next, val);
		return head->val == val ? head->next : head;
	}
};

首先对单链表是否是空的进行判断,此后将数值进行一个判断即刻。
3、反转一个单链表
C语言

struct ListNode*reverseList(struct ListNode*head)
{
	struct ListNode*prev = NULL;
	struct ListNode*curr = head;
	while (curr)
	{
		struct ListNode*next = curr->next;
		curr->next = prev;
		pre = curr;
		curr = next;
	}
	return prev;
}

c++

class Solution
{
public:
	ListNode* reverseList(ListNode*head)
	{
		ListNode*next = nullptr;
		ListNode*curr = head;
		while (curr)
		{
			ListNode*next = curr->next;
			curr->next = prev;
			prev = curr;
			curr = next;
		}
		rerurn prev;
	}
};

对单链表进行遍历,定义pre和curr两个指针实现链表反转。
在这里插入图片描述

4、给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点
c++

class Solution
{
public:
	ListNode*middleNode(ListNode*head)
	{
		vector<ListNode*>A = { head };
		while (A.back()->next != NULL)
			A.push_back(A.back()->next);
		return A[A.size() / 2];
	}
};

链表具有缺点不能通过下标访问对应的元素,因此我们直接对链表进行一个遍历,将元素放到数组中,对应求出元素的个数,[A.size()/2]就能将中间元素求出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值