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

移除链表元素

时间:2018-11-08 00:19:06      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:lis   ==   删除链表   elements   nts   solution   node   def   val   

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

示例:

输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5

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

移除链表元素

标签:lis   ==   删除链表   elements   nts   solution   node   def   val   

原文地址:https://www.cnblogs.com/yihangZhou/p/9926499.html

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