class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(-1), p = dummy;
ListNode p1 = l1, p2 = l2;
while (p1!=null && p2!=null){
if (p1.val<p2.val){
p.next = p1;
p1 = p1.next;
}
else{
p.next = p2;
p2 = p2.next;
}
p = p.next;
}
if (p1!=null){
p.next = p1;
}
if (p2!=null){
p.next = p2;
}
return dummy.next;
}
}
class Solution {
public ListNode partition(ListNode head, int x) {
ListNode p0 = head;
ListNode dummy1 = new ListNode(-1), p1 = dummy1;
ListNode dummy2 = new ListNode(-1), p2 = dummy2;
while(p0 != null){
if(p0.val < x){
p1.next = p0;
p1 = p1.next;
p0 = p0.next;
p1.next = null;
}
else{
p2.next = p0;
p2 = p2.next;
p0 = p0.next;
p2.next = null;
}
}
p1.next = dummy2.next;
return dummy1.next;
}
}
- lists.length 求长度不需要加括号
- // 优先队列的用法
- PriorityQueue<ListNode> pq = new PriorityQueue<>(lists.length,(a,b)->(a.val-b.val));
- // 定义方式
- ListNode dummy = new ListNode(-1);
/**
* 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 mergeKLists(ListNode[] lists) {
if(lists.length==0){
return null;
}
ListNode dummy = new ListNode(-1);
ListNode p = dummy;
PriorityQueue<ListNode> pq = new PriorityQueue<>(lists.length,(a,b)->(a.val-b.val)); // 如果是负数,a小
for (ListNode head : lists){
if(head!=null){
pq.add(head);
}
}
while(!pq.isEmpty()){
ListNode temp = pq.poll();
p.next = temp;
if(temp.next!=null){
pq.add(temp.next);
}
p = p.next;
p.next = null;
}
return dummy.next;
}
}
我的方法:
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode p1 = head;
ListNode p2 = new ListNode(0, head);
ListNode p3 = p2;
// 安全,这里我的思路是只有走n-1步骤,才会正确达到第n个数,所以我的p1一直指向了安全区域
for (int i=0;i<(n-1);i++){
p1 = p1.next;
}
// 当p1的下一个是null,那说明p1到了结尾节点
while(p1.next!=null){
p1 = p1.next;
p2 = p2.next;
}
// 在此过程中 p1 和 p2 的长度一直为n+1,所以当p1到达结尾,p2在倒数n+1上停着,这时将p2后边的数字删除掉即可
// 在删除的时候尤其注意,不能直接返回head,否则当只有一个值的时候,永远无法被删除,所以需要重新开启一个链表即p2,最后的返回结果也是p2
p2.next = (p2.next).next;
return p3.next;
}
}
答案结果
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0, head);
ListNode first = head;
ListNode second = dummy;
// first可以指向n个数的后边null,first和second之间的距离是n+2,所以需要first到达null,second才到达n+1
for (int i = 0; i < n; ++i) {
first = first.next;
}
while (first != null) {
first = first.next;
second = second.next;
}
second.next = second.next.next;
ListNode ans = dummy.next;
return ans;
}
}
class Solution {
public ListNode middleNode(ListNode head) {
ListNode p1 = head;
ListNode p2 = head;
while(p2.next!=null){
p1 = p1.next;
p2 = p2.next;
if(p2.next!=null){
p2 = p2.next;
}
}
return p1;
}
}
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode p1 = head;
ListNode p2 = head;
if (head==null) return null;
while(true){
if(p2.next == null){
return null;
}
else if(p2.next.next == null){
return null;
}
else{
p1 = p1.next;
p2 = p2.next.next;
}
if (p1 == p2){
break;
}
}
ListNode p3 = head;
while(p2!=p3){
p2 = p2.next;
p3 = p3.next;
}
return p2;
}
}
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode p1 = headA;
ListNode p2 = headB;
int flag1 = 0;
int flag2 = 0;
if (headA == null || headB == null) return null;
while(p1 != null || flag1==0){
if (flag1==0 && p1 == null){
p1 = headB;
flag1 = 1;
}
if(flag2==0 && p2 == null){
p2 = headA;
flag2 = 1;
}
if (p1 == p2) return p1;
p1 = p1.next;
p2 = p2.next;
}
return null;
}
}
官方题解
最开始不太理解,为什么没有处理 第一次为null和第二次为null的情况,
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) return null;
ListNode pA = headA, pB = headB;
while (pA != pB) {
pA = pA == null ? headB : pA.next;
pB = pB == null ? headA : pB.next;
}
return pA;
}
}