Lintcode 入门-452. 删除链表中的元素

论坛 期权论坛 脚本     
已经匿名di用户   2022-5-29 19:04   1231   0
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */


class Solution {
public:
    /*
     * @param head: a ListNode
     * @param val: An integer
     * @return: a ListNode
     */
    ListNode * removeElements(ListNode * head, int val) {
  if(head != NULL)
  {
   ListNode * temp = head;
   ListNode * result = NULL,* res = NULL;
   for(;temp != NULL;temp = temp->next)
   {
    if(temp->val != val)
    {
     if(res != NULL)
     {
      res->next = temp;
      res = res->next;
     }
     else
     {
      res = temp;
      result = temp;
     }
    }
   }
   if(res != NULL)
   res->next = NULL;
   return result;
  }
  return NULL;
    }
};
简单但没有一次过,认知:①随时注意逻辑的死角,一定要确保所有的地方都考虑到了
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP