1007. remove for single link list with head node
来源:互联网 发布:公务员论坛qzzn 软件 编辑:程序博客网 时间:2024/06/06 23:19
带虚拟头结点的单链表结点结构如下:
struct ListNode
{
int data;
ListNode *next;
};
链表类接口如下:
class List
{
public:
List()
{
head = new ListNode;
head->next = NULL;
}
~List(){ ListNode* curNode; while( head->next ) { curNode = head->next; head->next = curNode->next; delete curNode; } delete head;}//在链表第pos(pos>0)个结点之前插入新结点,新结点值为toadd //链表实际结点从1开始计数。//调用时需保证pos小等于链表实际结点数 void insert(int toadd, int pos) //删除链表的第pos(pos>0)个结点 //链表实际结点从1开始计数。 //调用时需保证pos小等于链表实际结点数 void remove(int pos);// Data fieldListNode *head; //head指向虚拟头结点,head-next指向第一个实际结点
};
请实现如下函数:
void List::remove(int pos)
只提交remove函数实现,不要提交类定义及main函数。
Problem Source: 平时测验1
我的代码
// Problem#: 19147// Submission#: 4809302// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen Universityvoid List::insert(int toadd, int pos) { ListNode *pre = head; if (pos <= 0) return; while (--pos && pre->next != NULL) { pre = pre->next; } ListNode * add = new ListNode; add->data = toadd; if (pre->next != NULL) { add->next = pre->next; } else { add->next = NULL; } pre->next = add;}
0 0
- 1007. remove for single link list with head node
- 1006. insert for single link list with head node
- [c++] list with head node
- Single link list
- Another Single Link List
- 【LeetCode with Python】 Remove Nth Node From End of List
- The Link List with C
- 237-delete node in single list
- The last Node of Link List
- single link
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node from End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- angularjs service全局保存数据的误区
- win系统下nodejs安装及环境配置
- 第8周项目3 顺序串算法(4)
- 深度学习中的数学与技巧(7):特征值和特征向量的几何意义、计算及其性质
- 3516A/D千兆网络变百兆方法
- 1007. remove for single link list with head node
- 第八周项目5—删除字符
- 第八周项目3-顺序串算法
- (第八周项目1)建立顺序串算法库
- Android 官方推荐 : DialogFragment 创建对话框
- 第8周项目3-顺序串算法(2)
- 【NOIP 2012】Vigenère 密码
- python sort、sorted高级排序技巧
- 第8周项目2-建立链串算法库