链表的习题(一)

这篇博客介绍了如何进行链表操作,包括删除指定值的节点、链表逆置、合并两个有序链表并保持有序,以及利用快慢指针找到单链表的中间或第二个中间节点。

1.删除链表中值为v的结点

  • 保存值为v节点的前一个节点
  • 释放置为v的节点
  • 保存的节点指向值为v节点的下一个节点地址
void SListRemove(SList *s, SListDataType v) {
	if (s->first == NULL) {
		return;
	}
	if (s->first->value == v) {
		Node *second = s->first->next; //记录原来的第二个结点
		free(s->first);//释放第一个结点空间
		s->first = second;//原来的第二个变为第一个
	}
	else {
		Node *c = s->first;
		while (c->next != NULL) {
			if (c->next->value == v) {
				Node *next = c->next;
				c->next = c->next->next;
				free(next);
				return;
			}
			c = c->next;
		}
	}
}

2.链表逆置(1)

struct ListNode* reverseList(struct ListNode* head) {
	struct ListNode *result = NULL;
	struct ListNode *c = head;
	while (c != NULL) {
		struct ListNode *next = c->next;
		c->next = result;
		result = c;
		c = next;
	}
	return result;
}

3.合并两个有序链表,合并后仍然有序

  • 如果有一个链表为空,则返回另一个链表
  • 比较两个链表节点值的大小,小的尾插到新链表中
  • 比较完,一个链表若仍有剩余,则全部尾插到新链表中
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode *l2) {
	if (l1 == NULL) { return l2; }
	if (l2 == NULL) { return l1; }
	struct ListNode *c1 = l1;
	struct ListNode *c2 = l2;
	struct ListNode *result = NULL;//新链表的第一个结点
	struct ListNode *tail = NULL;//新链表的最后一个结点
	while (c1 != NULL && c2 != NULL) {
		if (c1->val <= c2->val) {
			if (tail != NULL) {
				tail->next = c1;
				tail = c1;
			}else{
				result = tail = c1;
			}
			c1 = c1->next;
		}
		else {
			if (tail != NULL) {
				tail->next = c2;
				tail = c2;
			}
			else {
				result = tail = c2;
			}
			c2 = c2->next;
		}
		}
	if (c1 != NULL) {
		tail->next = c1;
	}
	if (c2 != NULL) {
		tail->next = c2;
	}
	return result;
}

4.查找单链表的中间结点,偶数返回第二个   (快慢指针)

struct ListNode* middleNode(struct ListNode* head) {
	struct ListNode *fast = head;
	struct ListNode *slow = head;
	while (fast != NULL) {
		fast = fast->next;
		if (fast == NULL) {
			break;
		}
		slow = slow->next;
		fast = fast->next;
	}
	return slow;
}

 

以下是些C++链表习题及对应代码示例: 1. **反转链表**:将个单链表反转。 ```cpp /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */ class Solution { public: ListNode* reverseList(ListNode* head) { ListNode *cur = head, *pre = nullptr, *next = nullptr; while(cur) { next = cur->next; cur->next = pre; pre = cur; cur = next; } return pre; } }; ``` 此代码通过迭代的方式,依次改变链表节点的指向,实现链表反转 [^1]。 2. **反转链表(另种实现)**: ```cpp struct ListNode { int val; struct ListNode *next; }; typedef struct ListNode LN; struct ListNode* reverseList(struct ListNode* head) { if (head==NULL) return head; LN *n1, *n2, *n3; n1 = NULL; n2 = head; n3 = head->next; while(n2) { n2->next = n1; n1 = n2; n2 = n3; if(n3) n3 = n3->next; } return n1; } ``` 同样是反转链表的功能,采用不同的变量命名和逻辑流程 [^2]。 3. **查找两个链表的交点**:找出两个单链表相交的起始节点。 ```cpp struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) { struct ListNode *cur1 = headA; struct ListNode *cur2 = headB; int countA = 0, countB = 0; while(cur1) { ++countA; cur1 = cur1->next; } while(cur2) { ++countB; cur2 = cur2->next; } //此时的count就记录了两个链表的长度 cur1 = headA; cur2 = headB; int gap = abs(countA - countB); if(countA < countB) //B链更长,应该B先走差距步,让俩个链表起始位置样 { while(gap--) { cur2 = cur2->next; } } else { while(gap--) { cur1 = cur1->next; } } //走到这两个链表就是样长 //假设两个链表相交那么走会在末尾之前找到个节点,两个val样 while(cur1) { if(cur1 == cur2) { return cur2; } else { cur1 = cur1->next; cur2 = cur2->next; } } return NULL; } ``` 该代码先计算两个链表的长度,然后让长链表的指针先走长度差的步数,最后同时移动两个指针,找到相交节点 [^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值