删除链表的节点
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15ListNode* deleteNode(ListNode* head, int val) { ListNode* dummy = new ListNode(0); //构建虚拟头节点 dummy->next = head; ListNode *t=dummy; while(t&&t->next){ if(t->next->val==val){ t->next=t->next->next; } else{ t=t->next; } } return dummy->next; }
合并两个排序的链表
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if(l1==NULL&&l2==NULL){ return NULL; } ListNode *res = new ListNode(0); ListNode *head=res; while(l1&&l2){ if(l1->val<l2->val){ head->next=l1; l1=l1->next; } else{ head->next=l2; l2=l2->next; } head=head->next;//向后移一位 } if(l1){ head->next=l1; } if(l2){ head->next=l2; } return res->next; }
最后
以上就是激昂羽毛最近收集整理的关于LeetCode-【链表】解题技巧的全部内容,更多相关LeetCode-【链表】解题技巧内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复