记录刷力扣时遇到的技巧(链表)
/*
* 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; }
*/
ListNode header = new ListNode(-1);
header.next = head;
ListNode cur = header;
链表反转
public static ListNode reverseListIterative(ListNode head) {
ListNode prev = null; //前指针节点
ListNode curr = head; //当前指针节点
//1->2->3->4->5
//5->4->3->2->1
//1<-2<-3<-4<-5
//每次循环,都将当前节点指向它前面的节点,然后当前节点和前节点后移
while (curr != null) {
ListNode nextTemp = curr.next; //临时节点,暂存当前节点的下一节点,用于后移
curr.next = prev; //将当前节点指向它前面的节点
prev = curr; //前指针后移
curr = nextTemp; //当前指针后移
}
return prev;
}
【92】给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。
class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode pre = dummy;
for(int i = 1; i < m; i++){
pre = pre.next;
}
head = pre.next;
for(int i = m; i < n; i++){
ListNode nex = head.next;
head.next = nex.next;
nex.next = pre.next;
pre.next = nex;
}
return dummy.next;
}
}
将给出的链表中的节点每 k 个一组翻转,返回翻转后的链表
如果链表中的节点数不是 k 的倍数,将最后剩下的节点保持原样
你不能更改节点中的值,只能更改节点本身。
public class Solution {
public ListNode reverseKGroup (ListNode head, int k) {
// write code here
if( k == 0 || k == 1 || head == null ) return head ;
ListNode pre = new ListNode(0) ; // 前一个节点
ListNode dummy = pre ; //第一个节点
ListNode temp , cur = head ;
dummy.next = head ;
int len = 0 ;
while (head != null) {
len ++ ;
head = head.next;
}
// 0->1->2->3->4->5
// 0->3->2->1->4->5
for(int j = 0 ; j < len / k ; j ++){
for(int i = 1 ; i < k ; i ++ ){
temp = cur.next;
cur.next = temp.next;
temp.next = pre.next;
pre.next = temp;
}
pre = cur ;
cur = cur.next ;
}
return dummy.next ;
}
}
判断链表中是否有环
判断给定的链表中是否有环。如果有环则返回true,否则返回false。
两种方法,哈希表和快慢指针
public boolean hasCycle(ListNode head) {
// 哈希
HashMap<ListNode,Integer> hash = new HashMap();
while(head != null ){
if(hash.containsKey(head))
return true ;
hash.put(head,1) ;
head = head.next ;
}
return false ;
//快慢指针
if(head == null ) return false ;
ListNode left = head , right = head ;
// 如果最后一个节点有环,那么又回到前面了
while(right != null && right.next != null ){
left = left.next ;
right = right.next.next;
if(left == right ) return true ;
}
return false ;
}
如果想知道在何处相遇呢?
两个结论:
1、设置快慢指针,假如有环,他们最后一定相遇。
2、两个指针分别从链表头和相遇点继续出发,每次走一步,最后一定相遇与环入口。
证明结论1:设置快慢指针fast和low,fast每次走两步,low每次走一步。假如有环,两者一定会相遇(因为low一旦进环,可看作fast在后面追赶low的过程,每次两者都接近一步,最后一定能追上)。
证明结论2:
设:
链表头到环入口长度为–a
环入口到相遇点长度为–b
相遇点到环入口长度为–c
则:相遇时
快指针路程=a+(b+c)k+b ,k>=1 其中b+c为环的长度,k为绕环的圈数(k>=1,即最少一圈,不能是0圈,不然和慢指针走的一样长,矛盾)。
慢指针路程=a+b
快指针走的路程是慢指针的两倍,所以:
(a+b)*2=a+(b+c)k+b
化简可得:
a=(k-1)(b+c)+c 这个式子的意思是: 链表头到环入口的距离=相遇点到环入口的距离+(k-1)圈环长度。其中k>=1,所以k-1>=0圈。所以两个指针分别从链表头和相遇点出发,最后一定相遇于环入口。
在后面加入代码:
if(right==null||right.next==null)
return null;
left=pHead;
while(right!=left){
right=right.next;
left=left.next;
}
return right
输入两个无环的单向链表,找出它们的第一个公共结点,如果没有公共节点则返回空。
思路:先将长的链表头部移动到与短链表相同长度的位置,然后同时移动
给出一个升序排序的链表,删除链表中的所有重复出现的元素,只保留原链表中只出现一次的元素。
例如:
给出的链表为1 \to 2\to 3\to 3\to 4\to 4\to51→2→3→3→4→4→5, 返回1\to 2\to51→2→5.
给出的链表为1\to1 \to 1\to 2 \to 31→1→1→2→3, 返回2\to 32→3.
public ListNode deleteDuplicates (ListNode head) {
// write code here
if(head == null || head.next == null) return head ;
ListNode dummy = new ListNode(0) , cur = dummy ;
dummy.next = head ;
while(head!=null && head.next != null){
if(head.val == head.next.val){
while(head.next != null && head.val == head.next.val){
head = head.next ;
}
cur.next = head.next ;
}
else{
cur = head ;
}
head = head.next ;
}
return dummy.next ;
}