• 203. Remove Linked List Elements(LeetCode)


    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  *     ListNode *next;
     6  *     ListNode(int x) : val(x), next(NULL) {}
     7  * };
     8  */
     9 class Solution {
    10 public:
    11     ListNode* removeElements(ListNode* head, int val) {
    12         if(head==NULL)
    13             return head;
    14         else
    15         {  
    16             while(head&&head->val==val)
    17             {
    18                 head=head->next;
    19             }
    20         }
    21         ListNode *p = head;
    22         ListNode *p1;
    23         while (p)
    24             {
    25                 if (p->val == val)
    26                 {
    27                     p = p->next;
    28                     p1->next=p;
    29                 }
    30                 else
    31                 {
    32                     p1 = p;
    33                     p = p->next;    
    34                 }
    35             }
    36         
    37         return head;
    38     }
    39 };
  • 相关阅读:
    Java注释
    加强版记事本
    Git简单使用
    Git简单使用
    Hadoop 2.6.0动态添加节点
    Hadoop 2.6.0动态添加节点
    ZooKeeper3.4.6配置
    ZooKeeper3.4.6配置
    SSH连接问题
    SSH连接问题
  • 原文地址:https://www.cnblogs.com/wujufengyun/p/7126675.html
Copyright © 2020-2023  润新知