Linklist
定义:node includes pointer(next) and value
区别(与Array):Array has indice,which is in sequence,LinkList has pointer to next.
single list and double list.
package Linkedlist;
public class ListNode {
ListNode next=null;
int val;
public ListNode(int d){
this.val=d;
}
public void appenttotail(int d){
ListNode end=new ListNode(d);
ListNode n=this;
while(n.next!=null){
n=n.next;
}
n.next=end;
}
public ListNode DelectNode(ListNode head,int d){
ListNode n=head;
if(n.next==null){
return head.next;
}else{
while(n.next!=null){
if(n.val==d){
n.next=n.next.next;
return head;
}
n=n.next;
}
return head;
}
}
}
2.Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
package Linkedlist; public class ADD2NUM { public ListNode addTwoNumbers(ListNode head1, ListNode head2) { if(head1==null){ return head2; } if(head2==null){ return head1; } int carry=0; ListNode newhead=new ListNode(-1); ListNode res=newhead; while(head1!=null||head2!=null){ if(head1!=null) { carry=carry+head1.val; head1=head1.next; } if(head2!=null) { carry=carry+head2.val; head2=head2.next; } res.next=new ListNode((carry)%10); carry=carry/10; res=res.next; } if(carry==1) { res.next =new ListNode(1); } return newhead.next; } }
19.Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.
暴力版
public class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNode res=head; ListNode fina=res; if(head==null||(head.next==null&&n==1)){ return null; } if(n==0){ return head; } int count=0; if(n==1){ while(head.next.next!=null){ head=head.next; } head.next=null; return fina; } if(n!=1){ while(head!=null){ head=head.next; count++; } } if(n==count){ return fina.next; } int j=1; while(res.next!=null){ if(j==(count-n)){ res.next=res.next.next; return fina; } res=res.next; j++; } return fina; } }
双指针
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNode res=head; ListNode fal=head; if(head==null||(head.next==null&&n==1)){ return null; } if(n==0){ return head; } for(int i=0;i<n;i++){ res=res.next; } if(res==null){ return head.next; } while(res.next!=null){ res=res.next; fal=fal.next; } fal.next=fal.next.next; return head; } }
Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode newhead=new ListNode(0); ListNode l3=newhead; if(l1==null){ return l2; } if(l2==null){ return l1; } while(l1!=null&&l2!=null){ if(l1.val<l2.val){ l3.next=new ListNode(l1.val); l1=l1.next; }else{ l3.next=new ListNode(l2.val); l2=l2.next; } l3=l3.next; } if(l1==null&&l2!=null){ l3.next=l2; } if(l2==null&&l1!=null){ l3.next=l1; } return newhead.next; } }
Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.归并操作(merge),也叫归并算法,指的是将两个已经排序的序列合并成一个序列的操作。归并排序算法依赖归并操作。
归并操作的过程如下:
- 申请空间,使其大小为两个已经排序序列之和,该空间用来存放合并后的序列
- 设定两个指针,最初位置分别为两个已经排序序列的起始位置
- 比较两个指针所指向的元素,选择相对小的元素放入到合并空间,并移动指针到下一位置
- 重复步骤3直到某一指针到达序列尾
- 将另一序列剩下的所有元素直接复制到合并序列尾
最差時間複雜度 | |
---|---|
最優時間複雜度 | |
平均時間複雜度 | |
最差空間複雜度 |
public int[] mergeSort(int[] arr){ if(arr.length<2||arr == null) return arr; MSort(arr,0,arr.length-1); } public int[] MSort(int[] arr, int low, int high){ if(low < high){ int mid = (low+high)/2; int[] left = MSort(arr,low,mid); int[] right = MSort(arr,mid+1,high); return mergeTwoList(left,right); } } public int[] mergeTwoList(int[] A, int[] B) { int[] C = new int[A.length + B.length]; int k = 0; int i = 0; int j = 0; while(i < A.length && j < B.length) { if (A[i] < B[j]) C[k++] = A[i++]; else C[k++] = B[j++]; } while (i < A.length) C[k++] = A[i++]; while (j < B.length) C[k++] = B[j++]; return C; }
Merge k Sorted Lists
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode mergeKLists(ListNode[] lists) { ListNode res; if(lists.length<=0){ return null; } if(lists.length==1){ return lists[0]; } res=mergeKlist(lists,0,lists.length-1); return res; } public ListNode mergeKlist(ListNode[] lists,int low,int high){ if(low<high){ int mid=(low+high)/2; ListNode left=mergeKlist(lists,low,mid); ListNode right=mergeKlist(lists,mid+1,high); return mergetwo(left,right); } return lists[low]; } public ListNode mergetwo(ListNode l1,ListNode l2){ ListNode newhead=new ListNode(0); ListNode l3=newhead; if(l1==null){ return l2; } if(l2==null){ return l1; } while(l1!=null&&l2!=null){ if(l1.val>l2.val){ l3.next=l2; l2=l2.next; }else{ l3.next=l1; l1=l1.next; } l3=l3.next; } if(l1!=null){ l3.next=l1; } if(l2!=null){ l3.next=l2; } return newhead.next; } }