关闭

(LeetCode)Remove Linked List Elements --- 删除节点元素

880人阅读 评论(0) 收藏 举报
分类:

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

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

Subscribe to see which companies asked this question


解题分析:

对于此题目,理解还是很容易理解的,就是对于给出的 List,

删除所有给出的VAL元素,再返回LinkedList

编程思维就是,对于给定的LinkedList, 先保存最开始的结点,然后依次遍历,判断是否是给出的val,

是的话,就删除。

# -*- coding:utf-8 -*-
__author__ = 'jiuzhang'
# Definition for singly-linked list.
class ListNode(object):
     def __init__(self, x):
         self.val = x
         self.next = None

class Solution(object):
    def removeElements(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """
        tmp = ListNode(0)
        tmp.next = head
        pre, cur = tmp, head
        while cur:
            if cur.val == val:
                pre.next = cur.next
            else:
                pre = cur
            cur = cur.next
        return tmp.next








0
0
查看评论

203. Remove Linked List Elements [easy] (Python)

题目链接https://leetcode.com/problems/remove-linked-list-elements/题目原文 Remove all elements from a linked list of integers that have value val. Exam...
  • coder_orz
  • coder_orz
  • 2016-06-18 16:30
  • 2858

203. Remove Linked List Elements Leetcode Python

Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --...
  • hyperbolechi
  • hyperbolechi
  • 2015-05-03 06:18
  • 1082

203. Remove Linked List Elements(python)

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...
  • qq_28119401
  • qq_28119401
  • 2016-09-18 21:39
  • 280

leetcode 203: Remove Linked List Elements

Remove Linked List Elements Total Accepted: 2921 Total Submissions: 10427 Remove all elements from a linked list of integers that have value val...
  • xudli
  • xudli
  • 2015-04-25 06:47
  • 4804

【LeetCode】 203. Remove Linked List Elements C语言

/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* removeElements...
  • godlovelong
  • godlovelong
  • 2017-04-18 09:11
  • 192

【LeetCode-面试算法经典-Java实现】【203-Remove Linked List Elements(删除单链表中的元素)】

【203-Remove Linked List Elements(删除单链表中的元素)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.com/Wang-Jun-Chao】原题  Remove all elements from a lin...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-26 07:21
  • 2420

LeetCode203:Remove Linked List Elements

Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --...
  • u012501459
  • u012501459
  • 2015-07-24 15:46
  • 963

[C语言][LeetCode][203]Remove Linked List Elements

题目Remove Linked List Elements Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 –> 6 –> 3 –>...
  • Timsley
  • Timsley
  • 2016-04-13 08:31
  • 578

Leetcode_203_Remove Linked List Elements

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/45868027 Remove all elements from a linked list of integers that have va...
  • pistolove
  • pistolove
  • 2015-05-20 10:57
  • 928

LeetCode-Easy部分中标签为LinkedList 203 Remove Linked List Elements

删除等于某个值的所有节点,返回删除后的单链表。 设立一个临时指针tmp指向链表头,假定头不等于要删除的节点,判断下一个节点是否等于,如果等于将tmp指向删除节点的下一个节点,。。。
  • daigualu
  • daigualu
  • 2017-04-06 14:28
  • 341
    个人资料
    • 访问:1596454次
    • 积分:29136
    • 等级:
    • 排名:第210名
    • 原创:1387篇
    • 转载:21篇
    • 译文:8篇
    • 评论:220条
    Social NetWork
    博客专栏
    最新评论