文章目录 1.搜索二维矩阵Ⅱ 2.相交链表 3.反转链表 4.回文链表 5.环形链表 6.环形链表Ⅱ 7.合并两个有序链表 8.两数相加 9.删除链表的倒数第n个节点 10.两两交换链表中的节点
1.搜索二维矩阵Ⅱ
当前值大于目标值,列--,小于当前值行++
class Solution { public boolean searchMatrix ( int [ ] [ ] matrix, int target) { int m = 0 , n = matrix[ 0 ] . length- 1 ; while ( m < matrix. length & & n > = 0 ) { if ( matrix[ m] [ n] > target) { n-- ; } else if ( matrix[ m] [ n] < target) { m++ ; } else { return true ; } } return false ; }
}
2.相交链表
a+c+b = b+c+a。走相同的路程
public class Solution { public ListNode getIntersectionNode ( ListNode headA, ListNode headB) { ListNode list1 = headA; ListNode list2 = headB; while ( list1 != list2) { list1 = list1 != null ? list1. next : headB; list2 = list2 != null ? list2. next : headA; } return list1; }
}
3.反转链表
注意:dummy必须是null,如果new的话默认一个元素为0
class Solution { public ListNode reverseList ( ListNode head) { ListNode dummy = null ; while ( head != null ) { ListNode temp = head. next; head. next = dummy; dummy = head; head = temp; } return dummy; }
}
4.回文链表
1.快慢指针找到中间节点的前一个节点
2.翻转后面部分节点
3.同时比较值是否相等
class Solution { public boolean isPalindrome ( ListNode head) { ListNode slow = head; ListNode fast = head. next; while ( fast != null && fast. next != null ) { slow = slow. next; fast = fast. next. next; } ListNode temp = slow. next; slow. next = null ; ListNode list2 = reverse ( temp) ; while ( head != null && list2 != null ) { if ( head. val == list2. val) { head = head. next; list2 = list2. next; } else { return false ; } } return true ; } public ListNode reverse ( ListNode head) { ListNode dummy = null ; while ( head != null ) { ListNode temp = head. next; head. next = dummy; dummy = head; head = temp; } return dummy; }
}
5.环形链表
快慢指针的应用:如果有环,其中快指针以相对速度为1接近慢指针
public class Solution { public boolean hasCycle ( ListNode head) { ListNode slow = head; ListNode fast = head; while ( fast != null && fast. next != null ) { slow = slow. next; fast = fast. next. next; if ( slow == fast) { return true ; } } return false ; }
}
6.环形链表Ⅱ
考察快慢指针
假设相交点为y 则(x+y) * 2 = x + y + n(y+z) ==> x + y = n(y+z) ==> x = (n-1)(y+z) + z == > x = z
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) { while ( slow != head) { slow = slow. next; head = head. next; } return slow; } } return null ; }
}
7.合并两个有序链表
双指针的应用
思路:构造新的链表,不断判断大小并且加入
class Solution { public ListNode mergeTwoLists ( ListNode list1, ListNode list2) { ListNode newList = new ListNode ( ) ; ListNode dummy = newList; while ( list1 != null && list2 != null ) { if ( list1. val < list2. val) { newList. next = list1; list1 = list1. next; } else { newList. next = list2; list2 = list2. next; } newList = newList. next; } if ( list1 != null ) newList. next = list1; if ( list2 != null ) newList. next = list2; return dummy. next; }
}
8.两数相加
考察携带因子,取余数等操作
class Solution { public ListNode addTwoNumbers ( ListNode l1, ListNode l2) { ListNode dummy = new ListNode ( ) ; ListNode dummy_head = dummy; int carry = 0 ; while ( l1 != null || l2 != null || carry != 0 ) { if ( l1 != null ) { carry += l1. val; l1 = l1. next; } if ( l2 != null ) { carry += l2. val; l2 = l2. next; } dummy. next = new ListNode ( carry % 10 ) ; carry = carry / 10 ; dummy = dummy. next; } return dummy_head. next; }
}
9.删除链表的倒数第n个节点
虚拟头节点的使用
注意:倒数第n个节点,可能是头节点,所以需要用虚拟头节点,避免单独考虑
class Solution { public ListNode removeNthFromEnd ( ListNode head, int n) { ListNode dummy = new ListNode ( ) ; dummy. next = head; ListNode fast = dummy; ListNode slow = dummy; while ( n > 0 ) { fast = fast. next; n-- ; } while ( fast. next != null ) { slow = slow. next; fast = fast. next; } slow. next = slow. next. next; return dummy. next; }
}
10.两两交换链表中的节点
定义first和second指针,注意顺序first要先指向后后面
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 first = pre. next; ListNode second = pre. next. next; pre. next = second; first. next = second. next; second. next = first; pre = first; } return dummy. next; }
}