Remove all elements from a linked list of integers that have valueval.
ExampleGiven:1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6,val= 6Return: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; }}
新闻热点
疑难解答