给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。
pre.next和pre.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) {
ListNode dummy = new ListNode();
dummy.next = head ;
ListNode pre = dummy ;
while(pre.next != null && pre.next.next !=null){
ListNode cur = pre.next ,next =cur.next ;
cur.next = next.next ;
next.next = cur;
//原指针指向最新的头节点
pre.next = next ;
//重新给pre引用复制
pre = cur ;
}
return dummy.next;
}
}
class Solution {
public ListNode swapPairs(ListNode head) {
if(head == null || head.next == null) return head ;
//每次交换两个元素,则新节点的头为当前头的下一个节点
ListNode newHead = head.next ;
//旧节点指向下一个新节点:每迭代一次得到当前节点的下一个新节点(已交换顺序)
head.next = swapPairs(newHead.next);
//新节点的下一个节点为旧节点的头
newHead.next = head ;
return newHead ;
}
}
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
return head.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 removeNthFromEnd(ListNode head, int n) {
if(head == null) return head;
ListNode slow = head ,fast = head,pre =null ;
while(n-->0){
fast = fast.next ;
}
if(fast == null) return head.next ;
while(fast != null){
pre = slow;
slow = slow.next ;
fast = fast.next ;
}
pre.next = slow.next ;
return head ;
}
}
给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。
/**
* 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;
Set<ListNode> sets = new HashSet<>();
ListNode cur = headA;
while(cur != null){
if(sets.contains(cur)){
return cur ;
}
sets.add(cur);
cur = cur.next ;
}
cur = headB ;
while(cur != null){
if(sets.contains(cur)){
return cur ;
}
sets.add(cur);
cur = cur.next ;
}
return null ;
}
}
参考倒数第N个节点,两个指针同时移动,第一个结束时,可以找到两个长度只差,交换指针位置,当两只真相同时,即找到相交节点
代码解读:
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode a = headA , b = headB ;
while(a !=b ){
a = a==null? headB :a.next;
b = b==null? headA :b.next;
}
return a;
}
}
给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。
不允许修改 链表。
/**
* 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 node = getInNodeLoop(head);
if(node == null){
return null;
}
ListNode tmp = head ;
while(tmp != node){
tmp = tmp.next;
node = node.next;
}
return node ;
}
//快慢指针得到任意相遇节点
public ListNode getInNodeLoop(ListNode head){
if(head == null || head.next == null){
return null;
}
ListNode slow = head.next ,fast=slow.next;
while(slow != null && fast != null){
if(slow == fast){
return slow ;
}
slow = slow.next ;
fast = fast.next ;
if(fast != null){
fast = fast.next ;
}
}
return null;
}
}