【英雄算法六月集训】Day11
328. 奇偶链表
/**
* 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 oddEvenList(ListNode head) {
if(head == null || head.next==null){
return head;
}
ListNode evenHead = head.next;
ListNode odd = head,even = evenHead;
while(even!=null&&even.next !=null){
odd.next = even.next;
odd = odd.next;
even.next = odd.next;
even = even.next;
}
odd.next = evenHead;
return head;
}
}
725. 分隔链表
/**
* 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 int count(ListNode head){
int cnt=0;
while(head!=null){
head = head.next;
++cnt;
}
return cnt;
}
public ListNode[] splitListToParts(ListNode head, int k) {
int listSize = count(head);
int i=0;
int per = listSize / k;
int rem = listSize % k;
List<ListNode> res= new ArrayList<ListNode>();
ListNode tmpHead = new ListNode();
tmpHead.next = head;
ListNode pre = tmpHead;
for(i=0;i<rem;++i){
res.add(head);
int c=per+1;
while(c-->=1){
pre = head;
head = head.next;
}
if(pre !=null){
pre.next = null;
}
}
for(i=0;i<k-rem;++i){
res.add(head);
int c = per;
while(c-->=1){
pre=head;
head = head.next;
}
if(pre!=null){
pre.next = null;
}
}
return res.stream().toArray(ListNode[]::new);
}
}
817. 链表组件
遍历到不在nums数组里的节点,我们应当认为需要重新开始一个新的组件,
/**
* 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 int numComponents(ListNode head, int[] nums) {
int[] hash= new int[10010];
int i,j;
for(i=0;i<nums.length;++i) ++hash[nums[i]];
int cnt=0,pre=0;
while(head!=null){
if(hash[head.val]>=1){
pre=1;
}else{
if(pre == 1){
++cnt;
}
pre =0;
}
head=head.next;
}
if(pre==1){
++cnt;
}
return cnt;
}
}
622. 设计循环队列
class MyCircularQueue {
ListNode front,rear;
int size,capacity;
public MyCircularQueue(int k) {
front=rear=null;
capacity=k;
size=0;
}
public boolean enQueue(int value) {
if(isFull()){
return false;
}
if(rear==null){
front=rear=new ListNode(val);
}else{
rear.next=new ListNode(val);
rear = rear.next;
}
++size;
return true;
}
public boolean deQueue() {
if(isEmpty()){
return false;
}
front=front.next;
--size;
if(size==0){
fron=rear=0;
}
return true;
}
public int Front() {
if(isEmpty()){
return -1;
}
return front.val;
}
public int Rear() {
if(isEmpty()){
return -1;
}
return rear.val;
}
public boolean isEmpty() {
return size==0;
}
public boolean isFull() {
return size == capacity;
}
}
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; }
}
/**
* Your MyCircularQueue object will be instantiated and called as such:
* MyCircularQueue obj = new MyCircularQueue(k);
* boolean param_1 = obj.enQueue(value);
* boolean param_2 = obj.deQueue();
* int param_3 = obj.Front();
* int param_4 = obj.Rear();
* boolean param_5 = obj.isEmpty();
* boolean param_6 = obj.isFull();
*/
class MyCircularQueue {
private int[] queue;
private int headIndex;
private int count;
private int capacity;
/** Initialize your data structure here. Set the size of the queue to be k. */
public MyCircularQueue(int k) {
this.capacity = k;
this.queue = new int[k];
this.headIndex = 0;
this.count = 0;
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
public boolean enQueue(int value) {
if (this.count == this.capacity)
return false;
this.queue[(this.headIndex + this.count) % this.capacity] = value;
this.count += 1;
return true;
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
public boolean deQueue() {
if (this.count == 0)
return false;
this.headIndex = (this.headIndex + 1) % this.capacity;
this.count -= 1;
return true;
}
/** Get the front item from the queue. */
public int Front() {
if (this.count == 0)
return -1;
return this.queue[this.headIndex];
}
/** Get the last item from the queue. */
public int Rear() {
if (this.count == 0)
return -1;
int tailIndex = (this.headIndex + this.count - 1) % this.capacity;
return this.queue[tailIndex];
}
/** Checks whether the circular queue is empty or not. */
public boolean isEmpty() {
return (this.count == 0);
}
/** Checks whether the circular queue is full or not. */
public boolean isFull() {
return (this.count == this.capacity);
}
}
作者:LeetCode
链接:https://leetcode.cn/problems/design-circular-queue/solution/she-ji-xun-huan-dui-lie-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。