力扣---2020.10.8
作者:互联网
344. 反转字符串
class Solution {
public void reverseString(char[] s) {
int left = 0,right = s.length-1;
while(left<right){
char c = s[left];
s[left] = s[right];
s[right] = c;
left++;
right--;
}
}
}
class Solution {
public void reverseString(char[] s) {
int n = s.length;
for(int i = 0;i < n/2;i++){
int j = n - 1 - i;
s[i] ^= s[j];
s[j] ^= s[i];
s[i] ^= s[j];
}
}
}
23. 合并K个升序链表
class Solution {
public ListNode mergeKLists(ListNode[] lists){
if(lists.length == 0)
return null;
if(lists.length == 1)
return lists[0];
if(lists.length == 2){
return mergeTwoLists(lists[0],lists[1]);
}
int mid = lists.length/2;
ListNode[] l1 = new ListNode[mid];
for(int i = 0; i < mid; i++){
l1[i] = lists[i];
}
ListNode[] l2 = new ListNode[lists.length-mid];
for(int i = mid,j=0; i < lists.length; i++,j++){
l2[j] = lists[i];
}
return mergeTwoLists(mergeKLists(l1),mergeKLists(l2));
}
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1;
ListNode head = null;
if (l1.val <= l2.val){
head = l1;
head.next = mergeTwoLists(l1.next, l2);
} else {
head = l2;
head.next = mergeTwoLists(l1, l2.next);
}
return head;
}
}
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists.length == 0) {
return null;
}
return merge(lists, 0, lists.length - 1);
}
private ListNode merge(ListNode[] lists, int lo, int hi) {
if (lo == hi) {
return lists[lo];
}
int mid = lo + (hi - lo) / 2;
ListNode l1 = merge(lists, lo, mid);
ListNode l2 = merge(lists, mid + 1, hi);
return merge2Lists(l1, l2);
}
private ListNode merge2Lists(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode tail = dummyHead;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
tail.next = l1;
l1 = l1.next;
} else {
tail.next = l2;
l2 = l2.next;
}
tail = tail.next;
}
tail.next = l1 == null? l2: l1;
return dummyHead.next;
}
}
19. 删除链表的倒数第N个节点
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if (head == null){
return null;
}
ListNode fast = head;
ListNode slow = head;
for (int i = 0;i < n;i++){
fast = fast.next;
}
if(fast==null){
return head.next;
}
while(fast.next != null){
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return head;
}
}
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode temp = head;
Map<Integer,ListNode> m = new HashMap<Integer,ListNode>();
int index=1;
while(temp!=null){
m.put(index++,temp);
temp = temp.next;
}
int size = m.size();
if(size == 1)
return null;
if(n == 1){
m.get(size-1).next = null;
}else if(n == size){
head = head.next;
}else{
m.get(size-n).next = m.get(size-n+2);
}
return head;
}
}
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode first = dummy;
ListNode second = dummy;
// Advances first pointer so that the gap between first and second is n nodes apart
for (int i = 1; i <= n + 1; i++) {
first = first.next;
}
// Move first to the end, maintaining the gap
while (first != null) {
first = first.next;
second = second.next;
}
second.next = second.next.next;
return dummy.next;
}
}
你知道的越多,你不知道的越多。
标签:---,head,ListNode,lists,next,力扣,2020.10,l1,return 来源: https://blog.csdn.net/qq_40722827/article/details/108967462