寻找第k个链表结点的值
快慢双指针
//https://leetcode-cn.com/problems/kth-node-from-end-of-list-lcci/submissions/
class Solution {
public int kthToLast(ListNode head, int k) {
ListNode slow=head;
ListNode fast=head;
while (fast!=null){
if(k<=0){
slow=slow.next;
}
k--;
fast=fast.next;
}
return slow.val;
}
}
旋转链表
//https://leetcode-cn.com/problems/rotate-list/
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head==null||head.next==null){
return head;
}
int n=0;
ListNode tail=head;
//寻找链表尾
while (tail.next!=null){
n++;
tail=tail.next;
}
n++;
//变环
tail.next=head;
//寻找新的链表头部
for(int i=1;i<=n-k%n;i++){
head=head.next;
}
tail=head;
//寻找尾部
for(int i=0;i<n-1;i++){
tail=tail.next;
}
tail.next=null;
return head;
}
}
删除某个链表结点
//https://leetcode-cn.com/problems/delete-middle-node-lcci/
class Solution {
public void deleteNode(ListNode node) {
if(node.next==null){
node=null;
}
ListNode next=node.next;
node.val=next.val;
node.next=node.next.next;
}
}
删除链表的结点
//https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/submissions/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
ListNode head1=new ListNode(0);
head1.next=head;
ListNode s=head1;
ListNode p=head;
while(p!=null){
if(p.val==val){
s.next=s.next.next;
return head1.next;
}else{
p=p.next;
s=s.next;
}
}
return head1.next;
}
}
复杂链表的复制
//https://leetcode-cn.com/problems/fu-za-lian-biao-de-fu-zhi-lcof/submissions/
class Solution {
public Node copyRandomList(Node head) {
HashMap<Node,Node> hashMap=new HashMap<>();
Node h=head;
while (h!=null){
hashMap.put(h,new Node(h.val));
h=h.next;
}
h=head;
while (h!=null){
hashMap.get(h).next=hashMap.get(h.next);
hashMap.get(h).random=hashMap.get(h.random);
h=h.next;
}
return hashMap.get(head);
}
}
合并有序链表
//https://leetcode-cn.com/problems/merge-two-sorted-lists/submissions/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head=new ListNode(0);
ListNode s=head;
while(l1!=null&&l2!=null){
if(l1.val<=l2.val){
s.next=l1;
s=s.next;
l1=l1.next;
}else{
s.next=l2;
s=s.next;
l2=l2.next;
}
}
while(l1!=null){
s.next=l1;
s=s.next;
l1=l1.next;
}
while(l2!=null){
s.next=l2;
s=s.next;
l2=l2.next;
}
return head.next;
}
}
两数相加
//https://leetcode-cn.com/problems/add-two-numbers/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int flag=0;
ListNode head=new ListNode(0);
ListNode p=head;
while (l1!=null||l2!=null){
int num1=0;
int num2=0;
num1=l1==null?0:l1.val;
num2=l2==null?0:l2.val;
int num=(num1+num2+flag)%10;
int a=(num1+num2+flag)/10;
ListNode s=new ListNode(num);
p.next=s;
p=p.next;
flag=a==0?0:1;
l1=l1==null?null:l1.next;
l2=l2==null?null:l2.next;
}
if(flag==1){
ListNode s=new ListNode(1);
p.next=s;
}
return head.next;
}
}
链表中点
//https://leetcode-cn.com/problems/middle-of-the-linked-list/submissions/
class Solution {
public ListNode middleNode(ListNode head) {
ListNode slow=head;
ListNode fast=head;
//快慢双指针找中点
int n=0;
while (fast!=null&&fast.next!=null){
n++;
fast=fast.next.next;
slow=slow.next;
}
return slow;
}
}
反转链表
//https://leetcode-cn.com/problems/reverse-linked-list/submissions/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode head1=new ListNode(0);
ListNode a=head1;
ListNode s=head,p=head;
while (s!=null){
p=s.next;
s.next=a.next;
a.next=s;
s=p;
}
return head1.next;
}
}
重排链表
//https://leetcode-cn.com/problems/reorder-list/
class Solution {
public void reorderList(ListNode head) {
ListNode head1=new ListNode(0);
ListNode p=head1;
ListNode slow=head;
ListNode fast=head;
//快慢双指针找链表中点
int n=0;
while (fast!=null&&fast.next!=null){
n++;
fast=fast.next.next;
slow=slow.next;
}
ListNode head2=reverse(slow);
//两条链表合并
head=merge(head,head2,n);
}
//合并链表
ListNode merge(ListNode head1,ListNode head2,int n){
ListNode head=new ListNode(0);
ListNode p=head;
for (int i=0;i<n;i++){
p.next=head1;
head1=head1.next;
p=p.next;
p.next=head2;
head2=head2.next;
p=p.next;
}
if(head2!=null){
p.next=head2;
}
return head.next;
}
//反转链表
ListNode reverse(ListNode head){
ListNode head1=new ListNode(0);
ListNode a=head1;
ListNode s=head,p=head;
while (s!=null){
p=s.next;
s.next=a.next;
a.next=s;
s=p;
}
return head1.next;
}
}
两两交换链表结点
//https://leetcode-cn.com/problems/swap-nodes-in-pairs/
class Solution {
public ListNode swapPairs(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode p1=head;
ListNode p2=head.next;
ListNode head1=new ListNode(0);
ListNode p=head1;
while (p1!=null&&p1.next!=null){
//先保留p1指针
ListNode p3=p1.next.next;
//防止p1变环
p1.next=null;
p.next=p2;
if(p2!=null&&p2.next!=null) {
p2 = p2.next.next;
}
p=p.next;
p.next=p1;
p1=p3;
p=p.next;
}
if(p1!=null){
p1.next=null;
p.next=p1;
}
return head1.next;
}
}
删除链表重复元素I
//https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode s=head;
ListNode p=head.next;
while(s!=null&&p!=null){
if(s.val==p.val){
s.next=p.next;
p=s.next;
}else{
s=s.next;
p=p.next;
}
}
return head;
}
}
删除重复链表II
//https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/submissions/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode a=new ListNode(0);
a.next=head;
ListNode pp=a; //s的前置指针
ListNode s=head;
ListNode p=head.next;
int flag=0; //标记是否有重复
while(s!=null&&p!=null){
flag=0;
while(s!=null&&p!=null&&s.val==p.val){
s=s.next;
p=p.next;
flag=1;
}
if(flag==1){ //删除结点
pp.next=p;
if(p!=null){
s=p;
p=p.next;
}
}else{
pp=pp.next;
s=s.next;
p=p.next;
}
}
return a.next;
}
}