解题--->链表方面的题目
1.移除链表
class Solution {
public ListNode removeElements(ListNode head, int val) {
while(head !=null && head.val==val){
head=head.next;
}
if(head==null){
return head;
}
ListNode preNode=head;
ListNode node=head.next;
while(node != null){
if(node.val==val){
preNode.next=node.next;
}else{
preNode=node;
}
node=node.next;
}
return head;
}
}
2.反转链表
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null){
return null;
}
ListNode preNode=null;
ListNode node=head;
while(node!=null){
ListNode nodeNext=node.next;
node.next=preNode;
preNode=node;
node=nodeNext;
}
return preNode;
}
}
3.链表的中间结点
class Solution {
public ListNode middleNode(ListNode head) {
ListNode node=head;
int count=0;
while(node!=null){
count++;
node=node.next;
}
int mid=count/2+1;
ListNode temp=head;
for(int i=1;i<mid;i++){
temp=temp.next;
}
return temp;
}
}
4.链表中倒数第k个结点
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(head==null){
return head;
}
ListNode fast=head;
ListNode slow=head;
for(int i=0;i<k;i++){
if(fast==null){
return null;
}
fast=fast.next;
}
while(fast!=null){
slow=slow.next;
fast=fast.next;
}
return slow;
}
}
5.合并两个有序链表
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if(list1==null && list2==null){
return null;
}
if(list1==null && list2!=null){
return list2;
}
if(list1!=null && list2==null){
return list1;
}
ListNode node=new ListNode(-1);
ListNode head=node;
ListNode temp1=list1;
ListNode temp2=list2;
while(temp1!=null && temp2!=null){
if(temp1.val<temp2.val){
head.next=temp1;
temp1=temp1.next;
}else{
head.next=temp2;
temp2=temp2.next;
}
head=head.next;
}
while(temp1!=null){
head.next=temp1;
temp1=temp1.next;
head=head.next;
}
while(temp2!=null){
head.next=temp2;
temp2=temp2.next;
head=head.next;
}
return node.next;
}
}
6.链表分割
public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
ListNode bigHead=new ListNode(-1);
ListNode smallHead=new ListNode(-1);
ListNode smallTail=smallHead;
ListNode bigTail=bigHead;
while(pHead!=null){
if(pHead.val<x){
smallTail.next=pHead;
smallTail=smallTail.next;
}else{
bigTail.next=pHead;
bigTail=bigTail.next;
}
pHead=pHead.next;
}
bigTail.next=null;
smallTail.next=bigHead.next;
return smallHead.next;
}
}
7.删除链表中重复的节点
public class Solution {
public static ListNode deleteDuplication(ListNode pHead) {
if(pHead==null || pHead.next==null){
return pHead;
}
ListNode root=new ListNode(-1);
root.next=pHead;
ListNode pre=root;
ListNode cur=root;
while(cur!=null){
while(cur!=null&& cur.next!=null && cur.val==cur.next.val){
cur=cur.next;
}
cur=cur.next;
if(cur!=null && cur.next!=null && cur.val==cur.next.val){
continue;
}
pre.next=cur;
pre=pre.next;
}
return root.next;
}
}
8.链表的回文结构
public class PalindromeList {
public static boolean chkPalindrome(ListNode A) {
// write code here
ListNode fast=A;
ListNode slow=A;
//找链表的中间节点,fast一次走两步,slow一次走一步
while(fast!=null && fast.next!=null){
fast=fast.next.next;
slow=slow.next;
}
//此时slow指的就是中间节点,现在需要进行对链表的后半部分进行链表反转
fast=slow.next;
while(fast!=null) {
ListNode fastNext = fast.next;
fast.next = slow;
slow = fast;
fast = fastNext;
}
while(slow!=A){
if(slow.val!=A.val){
return false;
}
if(A.next==slow){
return true;
}
if(A.val==slow.val){
A=A.next;
slow=slow.next;
}
}
return true;
}
}
9.环形链表
public class Solution {
public static boolean hasCycle(ListNode head) {
if(head==null || head.next==null){
return false;
}
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;
}
}
10.环形链表II
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast=head;
ListNode slow=head;
while(true){
if(fast==null || fast.next==null){
return null;
}
fast=fast.next.next;
slow=slow.next;
if(fast==slow){
break;
}
}
ListNode third=head;
while(third!=slow){
slow=slow.next;
third=third.next;
}
return third;
}
}