class Solution {
public static List<List<Integer>> generate(int numRows) {
List<List<Integer>> all = new ArrayList();
List<Integer> first = new ArrayList<>();
first.add(1);
all.add(first);
if(numRows >=2) {
List<Integer> second = new ArrayList<>();
second.add(1);
second.add(1);
all.add(second);
}
for(int i = 2;i < numRows; i++) {
List<Integer> row = new ArrayList<>();
row.add(1);
int start =0;
for(int j = 1; start<all.get(i-1).size()-1; j++) {
int a = all.get(i-1).get(start)+all.get(i-1).get(start+1);
start++;
row.add(a);
}
row.add(1);
all.add(row);
}
return all;
}
}
Monthly Archives: May 2021
19. Remove Nth Node From End of List
19. Remove Nth Node From End of List
/**
* 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 removeNthFromEnd(ListNode head, int n) {
int len = 0;
ListNode current = head;
while(current != null){
len++;
current = current.next;
}
int required = len - n;
if(required == 0) return head.next;
ListNode c = head;
for(int i =0; i < len; i++){
if(i == required-1) {
c.next = c.next.next;
}
else c = c.next;
}
return head;
}
}