乐虎游戏|乐虎国际登录|欢迎你

203. Remove Linked List Elements,linkedelements

日期:2019-11-03编辑作者:计算机资讯

203. Remove Linked List Elements,linkedelements

Remove all elements from a linked list of integers that have value val.

Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5

代码如下:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if(head==null)
        return null;

        while(head.val==val&&head.next!=null)
        head=head.next;

        if((head.next==null&&head.val==val))
        return null;

        ListNode p=head;

        while(p.next!=null)
        {
            if(p.next.val==val)
            p.next=p.next.next;
            else p=p.next;

        }
        return head;
    }
}

 

. Remove Linked List Elements,linkedelements Remove all elements from a linked list of integers that have value val . Example Given: 1 -- 2 -- 6 -- 3 -- 4 -- 5 -- 6, val = 6 R...

203. Remove Linked List Elements,linkedelements

Remove all elements from a linked list of integers that have value val.

Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 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     struct ListNode* cur;
10     struct ListNode* pre;
11     if(NULL == head)
12         return head;
13     cur = head;
14     pre = head;
15     while(cur != NULL)
16     {
17         if(val == head->val)               //注意删除头结点的情况!
18         {
19             head = head->next;
20             cur = head;
21             pre = head;
22             continue;
23         }
24         if(val == cur->val)
25         {
26             pre->next = cur->next;
27             cur = cur->next;
28             continue;
29         }
30         pre = cur;
31         cur = cur->next;
32     }
33     return head;
34 }

 

. Remove Linked List Elements,linkedelements Remove all elements from a linked list of integers that have value val . Example Given: 1 -- 2 -- 6 -- 3 -- 4 -- 5 -- 6, val = 6 R...

本文由乐虎游戏发布于计算机资讯,转载请注明出处:203. Remove Linked List Elements,linkedelements

关键词:

Spring中的Bean配置方式

Spring基础——在Spring Config 文件中配置 Bean,springbean 一、基于 XML 的 Bean 的配置 1.通过属性注入 即通过 setXxx() 方法注...

详细>>

java虚拟机总结,java虚拟机

Jvm 内存浅析 及 GC个人学习总计,jvmgc 从出生到现在,20多年过去,Java到现在仍为选取最为不感到奇的言语。那仰赖于...

详细>>

ag视讯直播spring mvc生成注册验证码,springmvc

bbin手机客户端,spring mvc生成注册验证码,springmvc 通过Spring MVC为系统添加验证码 1:布局登陆页面,用户名,密码,...

详细>>

MegaCli 监控raid状态

首先有必要先介绍一下有关RAID卡的使用,有关Cache问题也有个人理解:生产系统上的物理机磁盘配置大多都是RAID5,之...

详细>>