目录
(6)以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前
(10)给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回?NULL
在顺序表中任意位置插入或删除元素时,需要把元素整体往后或往前移动,时间复杂度为O(n)。因此顺序表不适用于任意位置插入或删除元素的场景。
在逻辑上是连续的,但在物理上不一定是连续的。
(1)单向
(2)双向
(3)无头
(4)有头
(5)循环
(6)不循环
是无头双向循环链表。
class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head==null){
return null;
}
ListNode pre=head;
ListNode cur=pre.next;
while(cur!=null){
if(cur.val==val){
pre.next=cur.next;
cur=cur.next;
}else{
pre=cur;
cur=cur.next;
}
}
if(head.val==val){
head=head.next;
}
return head;
}
}
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode cur=head.next;
head.next=null;
while(cur!=null){
ListNode curnext=cur.next;
cur.next=head;
head=cur;
cur=curnext;
}
return head;
}
}
class Solution {
public ListNode middleNode(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode slow=head;
ListNode fast=head;
while(fast!=null&&fast.next!=null){
slow=slow.next;
fast=fast.next.next;
}
return slow;
}
}
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(head==null) return null;
ListNode cur=head;
int size=0;
while(cur!=null){
size++;
cur=cur.next;
}
if(k<=0||k>size){
return null;
}
ListNode fast=head;
ListNode slow=head;
while(k-1!=0){
fast=fast.next;
k--;
}
while(fast.next!=null){
slow=slow.next;
fast=fast.next;
}
return slow;
}
}
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(k<=0||head==null){
return null;
}
ListNode fast=head;
ListNode slow=head;
int count=0;
//要么走k-1步结束循环,要么长度不够长退出循环
while(k-1!=count){
fast=fast.next;
if(fast==null){
return null;
}
count++;
}
while(fast.next!=null){
slow=slow.next;
fast=fast.next;
}
return slow;
}
}
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if(list1==null) return list2;
if(list2==null) return list1;
ListNode head1=new ListNode(0);
ListNode head2=head1;
while(list1!=null&&list2!=null){
if(list1.val<list2.val){
head2.next=list1;
head2=list1;
list1=list1.next;
}else{
head2.next=list2;
head2=list2;
list2=list2.next;
}
}
if(list1==null){
head2.next=list2;
}else{
head2.next=list1;
}
return head1.next;
}
}
public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
ListNode head1=null;
ListNode cur1=null;
ListNode cur2=null;
ListNode head2=null;
while(pHead!=null){
if(pHead.val<x){
if(head1==null){
head1=new ListNode(pHead.val);
cur1=head1;
}else{
cur1.next=pHead;
cur1=pHead;
}
}else{
if(head2==null){
head2=new ListNode(pHead.val);
cur2=head2;
}else{
cur2.next=pHead;
cur2=pHead;
}
}
pHead=pHead.next;
}
if(head2==null){
return head1;
}else if(head1==null){
return head2;
}else{
cur2.next=null;
cur1.next=head2;
return head1;
}
}
}
public class PalindromeList {
public boolean chkPalindrome(ListNode A) {
// write code here
if(A==null||A.next==null) return true;
ListNode slow=A;
ListNode fast=A;
ListNode B=null;
while(fast!=null&&fast.next!=null){
slow=slow.next;
fast=fast.next.next;
}
if(fast==null){
B=slow;
}else{
B=slow.next;
}
ListNode C=reverseList(B);
while(C!=null){
if(C.val!=A.val){
return false;
}
C=C.next;
A=A.next;
}
return true;
}
public ListNode reverseList(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode cur=head.next;
head.next=null;
while(cur!=null){
ListNode curnext=cur.next;
cur.next=head;
head=cur;
cur=curnext;
}
return head;
}
}
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA==null&&headB!=null) return null;
if(headA!=null&&headB==null) return null;
ListNode cur1=headA;
ListNode cur2=headB;
int size1=size(cur1);
int size2=size(cur2);
int difference=0;
if(size1>size2){
difference=size1-size2;
while(difference!=0){
cur1=cur1.next;
difference--;
}
}else{
difference=size2-size1;
while(difference!=0){
cur2=cur2.next;
difference--;
}
}
while(cur1!=null){
if(cur1==cur2){
return cur1;
}else{
cur1=cur1.next;
cur2=cur2.next;
}
}
return null;
}
public int size(ListNode cur){
int count=0;
while(cur!=null){
count++;
cur=cur.next;
}
return count;
}
}
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast=head;
ListNode slow=head;
while(fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
if(fast==slow)
return true;
}
return false;
}
}
public class Solution {
public ListNode detectCycle(ListNode head) {
if(head==null||head.next==null){
return null;
}
ListNode cur=head;
//先找到相遇点
ListNode slow=head;
ListNode fast=head;
while(fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
if(slow==fast) break;
}
if(fast==null||fast.next==null) return null;
ListNode cur1=slow;
//一个从相遇点出发,一个从起点出发,再次相遇时,相遇点为环的起点
while(cur!=cur1){
cur=cur.next;
cur1=cur1.next;
}
return cur;
}
}
nR-X=(n-1)R+R-x; L大,环小,多走几圈。