爱程序网

[LeetCode] 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 --> 6,  val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5

     这道题两个指针就可以了哇。不过我总是把指针搞混。所以我还是觉得有点绕T T

public class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode track = new ListNode(0);  
        track.next = head;  
        ListNode a = track;  
        ListNode b = head;  
        while(b!=null) {  
            if(b.val == val) {  
                a.next = b.next;  
            } else {  
                a = a.next;  
            }  
            b = b.next;  
        }  
          
        return track.next;  
    }
}

 

关于爱程序网 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助