Delete N Nodes After M Nodes of a Linked List
给一个链表, 删除m个node后边的n个node.
这个题就是corner cases多…
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode deleteNodes(ListNode head, int m, int n) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode cur = dummy;
while(cur != null){
int t = m;
int tt = n;
while(t-- > 0){
if(cur == null)
return head;
cur = cur.next;
}
while(tt -- >0){
if(cur == null || cur.next == null)
return head;
cur.next = cur.next.next;
}
}
return head;
}
}