24. 两两交换链表中的节点
这道题也是需要哨兵节点,并需要一个指向哨兵节点的指针来操作交换链表节点,只要一个节点一个节点挪动指针,该题思路就会很清晰,需要提前保存两个节点路径,一个是cur.next,另一个是cur.next.next.next,具体详细见代码。
/**
* 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 headA = new ListNode(0,head);
ListNode cur = headA;
while(cur.next != null && cur.next.next != null){
ListNode temp1 = cur.next.next.next;
ListNode temp2 = cur.next;
//先指明头节点
cur.next = cur.next.next;
cur.next.next = temp2;
temp2.next = temp1;
cur = temp2;
}
return headA.next;
}
}
19.删除链表的倒数第N个节点
该题请注意边界问题,也是曾经做过的题目
/**
* 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) {
if(head==null) return null;
ListNode headA = new ListNode(-1,head);
ListNode pre = headA;
ListNode cur = headA.next;
ListNode f = headA.next;
n--;
while(n != 0){
f = f.next;
n--;
}
while(f.next !=null){
pre = cur;
cur = cur.next;
f = f.next;
}
pre.next = cur.next;
return headA.next;
}
}
面试题 02.07. 链表相交
写法不太规范:
/**
* 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) {
if(headA == null || headB == null) return null;
ListNode cur1 = headA;
ListNode cur2 = headB;
if(headA == headB) return headA;
while(cur1.next != null || cur2.next != null){
cur1 = cur1.next == null?headB:cur1.next;
cur2 = cur2.next == null?headA:cur2.next;
if(cur1 == cur2) return cur1;
}
return null;
}
}
正常写法:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
class Solution {
ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA==null||headB==null){
return null;
}
ListNode l1 = headA;
ListNode l2 = headB;
while(l1!=l2){
l1 = l1==null?headB:l1.next;
l2 = l2==null?headA:l2.next;
}
return l1;
}
}
142.环形链表II
做过这道题,链接在此
/**
* 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) {
if(head == null || head.next == null) return null;
ListNode slow = head;
ListNode fast = head.next;
while(slow != fast){
slow = slow.next;
if(fast == null) return null;
fast = fast.next;
if(fast == null) return null;
fast = fast.next;
}
//循环链表节点个数计算
int num = 1;
//之前这块出现了问题,别忘了设置变量,slow是一直变化的
ListNode l = slow;
while(slow.next != l){
num++;
slow = slow.next;
}
slow = head;
fast = head;
while(num != 0){
fast = fast.next;
num--;
}
while(slow != fast){
slow = slow.next;
fast = fast.next;
}
return slow;
}
}