反转链表
//迭代法
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre=null;
while(head!=null){
ListNode next=head.next;
head.next=pre;
pre=head;
head=next;
}
return pre;
}
}
//递归法
class Solution {
public ListNode reverseList(ListNode head) {
return reverse(null,head);
}
public ListNode reverse(ListNode pre,ListNode hou){
if(hou==null){
return pre;
}
ListNode tmp=hou.next;
hou.next=pre;
return reverse(hou,tmp);
}
}
链表两两反转
class Solution {
public ListNode swapPairs(ListNode head) {
//递归法
// base case 退出提交
if(head == null || head.next == null) return head;
// 获取当前节点的下一个节点
ListNode next = head.next;
// 进行递归
ListNode newNode = swapPairs(next.next);
// 这里进行交换
next.next = head;
head.next = newNode;
return next;
}
}
class Solution {
public ListNode swapPairs(ListNode head) {
//迭代
ListNode dummyNode = new ListNode(0,head);
ListNode prev = dummyNode;
while (prev.next != null && prev.next.next != null) {
ListNode temp = head.next.next; // 缓存 next
prev.next = head.next; // 将 prev 的 next 改为 head 的 next
head.next.next = head; // 将 head.next(prev.next) 的next,指向 head
head.next = temp; // 将head 的 next 接上缓存的temp
prev = head; // 步进1位
head = head.next; // 步进1位
}
return dummyNode.next;
}
}
删除链表的倒数第N个节点
slow fast先走n步
奇偶链表组合
class Solution {
public ListNode oddEvenList(ListNode head) {
ListNode odd=new ListNode(0);
ListNode oven=new ListNode(0);
ListNode od=odd;
ListNode ov=oven;
while(head!=null&&head.next!=null){
od.next=head;
ov.next=head.next;
head=head.next.next;
od=od.next;
od.next=null;
ov=ov.next;
ov.next=null;
}
if(head!=null) {
od.next=head;
od=od.next;
}
od.next=oven.next;
return odd.next;
}
}
带random链表的复制
class Solution {
public Node copyRandomList(Node head) {
Node pp=new Node(-1);
Node pre=pp;
//旧的node,新的node
HashMap<Node,Node> hm=new HashMap();
Node p1=head;
while(p1!=null){
Node newNode=new Node(p1.val);
hm.put(p1,newNode);
pre.next=newNode;
pre=pre.next;
p1=p1.next;
}
Node p2=head;
Node p=pp.next;
while(p2!=null){
Node rand=p2.random;
Node curr=hm.get(rand);
p.random=curr;
p2=p2.next;
p=p.next;
}
return pp.next;
}
}
链表求和
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode pp=new ListNode(-1);
ListNode pre=pp;
int sum=0;
while(l1!=null&&l2!=null){
sum+=l1.val;
sum+=l2.val;
ListNode newNode=new ListNode(sum%10);
sum/=10;
pre.next=newNode;
l1=l1.next;
l2=l2.next;
pre=pre.next;
}
while(l1!=null){
sum+=l1.val;
pre.next=new ListNode(sum%10);
sum/=10;
pre=pre.next;
l1=l1.next;
}
while(l2!=null){
sum+=l2.val;
pre.next=new ListNode(sum%10);
sum/=10;
pre=pre.next;
l2=l2.next;
}
if(sum>=1){
pre.next=new ListNode(1);
}
return pp.next;
}
}
删除中间节点
class Solution {
public void deleteNode(ListNode node) {
node.val=node.next.val;
node.next=node.next.next;
}
}
K个一组翻转链表
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode pp=new ListNode(0);
ListNode pre=pp;
pp.next=head;
while(head!=null){
for(int i=0;i<k-1;i++){
head=head.next;
if(head==null) return pp.next;
}
//记录下一次反转的起始位置
ListNode head1=head.next;
ListNode left=pre.next;
ListNode[] tmp=reverse(left,head);
tmp[1].next=head1;
pre.next=tmp[0];
pre=tmp[1];
head=head1;
}
return pp.next;
}
//得到反转链表后的首尾节点的数组
public ListNode[] reverse(ListNode first,ListNode tail){
ListNode pre=null;
ListNode ln[]=new ListNode[2];
ln[1]=first;
while(first!=tail){
ListNode next=first.next;
first.next=pre;
pre=first;
first=next;
}
tail.next=pre;
ln[0]=tail;
return ln;
}
}
快速排序
class Solution {
public int[] sortArray(int[] nums) {
quickSort(nums,0,nums.length-1);
return nums;
}
public void quickSort(int arr[],int low,int high){
int i,j,temp,t;
if(low>high){
return;
}
i=low;
j=high;
//temp就是基准位
temp = arr[low];
while (i<j) {
//先看右边,依次往左递减
while (temp<=arr[j]&&i<j) {
j--;
}
//再看左边,依次往右递增
while (temp>=arr[i]&&i<j) {
i++;
}
//如果满足条件则交换
if (i<j) {
t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
//最后将基准为与i和j相等位置的数字交换
arr[low] = arr[i];
arr[i] = temp;
//递归调用左半数组
quickSort(arr, low, j-1);
//递归调用右半数组
quickSort(arr, j+1, high);
}
}