码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 203. 移除链表元素

时间:2019-08-16 22:50:31      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:next   nts   输出   http   list   def   删除链表   etc   null   

题目链接:https://leetcode-cn.com/problems/remove-linked-list-elements/

删除链表中等于给定值 val 的所有节点。

示例:

输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     struct ListNode *next;
 6  * };
 7  */
 8 struct ListNode* removeElements(struct ListNode* head, int val){
 9     while(head!=NULL&&head->val==val){
10         head=head->next;
11     }
12     if(head==NULL) return NULL;
13     struct ListNode *p=head;
14     while(p->next!=NULL){
15         if(p->next->val==val) p->next=p->next->next;
16         else p=p->next;
17     }
18     return head;
19 }

 

LeetCode 203. 移除链表元素

标签:next   nts   输出   http   list   def   删除链表   etc   null   

原文地址:https://www.cnblogs.com/shixinzei/p/11366385.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!