24: 第一眼看没什么思路,就直接去看文档了,主要掌握的知识是如何设置虚拟节点,这样非常方便操作。只需要设置一个新的Node,然后把他放到head的前面,就算是新的虚拟节点了。在while loop里面要注重每一步的逻辑顺序(画图最为清晰)。在这里设置两个temp主要是为了保留原来的值,这样swap就不会搞错了。
Code:
/**
* 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 swapPairs(ListNode head) {
if(head == null || head.next == null){
return head;
}
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode current = dummy;
while(current.next != null && current.next.next != null){
ListNode temp = current.next;
ListNode temp1 = current.next.next.next;
//step 1
current.next = current.next.next;
//step 2
current.next.next = temp;
//step 3
current.next.next.next = temp1;
//switch the current position
current = current.next.next;
}
return dummy.next;
}
}
用时:15min
19: 第一个思路大概就是用length-n算出到底要remove哪一个index的数值然后再用正常的remove, 但是发现只给了head,没办法找到length。看了提示之后发现要用两个pointer,于是想出来一个pointer先走n,然后两个在一起移动,直到先移动的pointer.next == null。然后第二个pointer就定位到了要移动的index的前一位。但是在测试的时候发现要加一个dummy node,这样可以避免要移走第一个index时候的麻烦。
Code:
/**
* 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) {
ListNode dummy = new ListNode(0);
dummy.next = head;
if(head == null){
return head;
}else if(head.next == null && n == 1){
return null;
}
ListNode first = dummy;
ListNode second = dummy;
for(int i=0; i<n; i++){
second = second.next;
}
while(second.next != null){
first = first.next;
second = second.next;
}
first.next = first.next.next;
return dummy.next;
}
}
160: 主要思路是要找出长度差,然后屁股对齐,这样就能看出来重合的pointer。注意要用ptr去指代head,要不然会乱。
Code:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
int lengthA = 0;
ListNode ptrA = headA;
while(ptrA != null){
ptrA = ptrA.next;
lengthA++;
}
int lengthB = 0;
ListNode ptrB = headB;
while(ptrB != null){
ptrB = ptrB.next;
lengthB++;
}
int diff = Math.abs(lengthA-lengthB);
ptrA = headA;
ptrB = headB;
if(lengthA > lengthB){
for(int i=0; i< diff; i++){
ptrA = ptrA.next;
}
}else{
for(int i=0; i< diff; i++){
ptrB = ptrB.next;
}
}
while(ptrA != null){
if(ptrA != ptrB){
ptrA = ptrA.next;
ptrB = ptrB.next;
}else{
return ptrA;
}
}
return null;
}
}
142: 主要是思路要想明白,多回顾思路
Code:
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast){
break;
}
}
if (fast == null || fast.next == null) return null;
while (head != slow) {
head = head.next;
slow = slow.next;
}
return head;
}
}