单链表删nodes
来源:互联网 发布:ar软件 编辑:程序博客网 时间:2024/05/17 03:37
///////////////////////////////////////////////////// C++ /////////////////////////////////////////////#include <iostream>#include <stdio.h>using namespace std;struct ListNode{int val;ListNode*next;};ListNode *removeNode(ListNode *firstNode, int removalRequests[], int removalRequests_Length){if (firstNode == nullptr)return firstNode;// dummy nodeListNode dummy{ -1, firstNode};// position node pointerListNode *current = firstNode;// previous node pointerListNode *previous = &dummy;// index for removal Requests arrayint index = 0;for (int count = 0; current != nullptr && index < removalRequests_Length; count++){// if match delete position nodeif (removalRequests[index] == count){// update indexindex++;// change the previous node next pointer to// next pointer of the node to be deleted.previous->next = current->next;free(current);current = previous->next;}else{previous = current;current = current->next;}}return dummy.next;}int main(){ListNode *head = new ListNode();head->val = 1;ListNode *q = head;ListNode *p = nullptr;for (int i = 2; i <= 10; i++) {p = new ListNode();p->val = i;q->next = p;q = q->next;p->next = nullptr;p = head;}cout << "目标链表:" << endl;while (p != NULL) {cout << p->val << " ";p = p->next;}cout << endl;int foo[2] = {1,3};ListNode *x = removeNode(head, foo, 2);cout << "结果链表:" << endl;while (x != NULL) {cout << x->val << " ";x = x->next;}cout << endl;}
0 0
- 单链表删nodes
- Exchange Nodes
- nodes(connected) == nodes(visible) ++ nodes(hidden).
- Narrowband Access Nodes
- Working with XML nodes
- Working with XML nodes
- XPath Nodes(节)
- ItemID - Finding Nodes
- Tree with CheckBox nodes
- JOJ2035: Leaf Nodes
- flex learn nodes
- HTML DOM Nodes
- Maya DG nodes笔记
- T-SQL XQUERY.NODES
- Swap Nodes in Pairs
- rac oracle single nodes
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- 【C语言】06-基本数据类型
- Java分层结构
- Java 8:HashMap的性能提升
- 实验七 :寻址方式在结构化数据访问中的应用
- object-c学习:协议protocol
- 单链表删nodes
- Java for-each循环解惑
- 《黑马程序员》 Tcp服务端及客户端通信
- 线程中的Semaphore学习,公共厕所排队策略
- [TroubleShootin]The backup set holds a backup of a database other than the existing 'xxdb' database.
- ASP数据库服务器SQL Server
- ASP.NET四种页面导航方式之比较与选择
- 百度快照相关问题
- 《黑马程序员》Udp之聊天室