1. 删除链表中等于给定值 val 的所有节点
public void deleteNode(ListNode node) {
ListNode nodeNext = node.next;
node.next = nodeNext.next;
node.val = nodeNext.val;
}
2. 反转一个单链表
public ListNode reverseList(ListNode head) {
if(head == null){
return null;
}
if(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;
}
3. 给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点
public ListNode middleNode(ListNode head) {
if(head == null){
return null;
}
if(head.next == null){
return head;
}
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
4. 输入一个链表,输出该链表中倒数第k个结点
public ListNode FindKthToTail(ListNode head,int k) {
if(k<=0||head==null){
return null;
}
ListNode fast = head;
ListNode slow = head;
while(k-1>0){
fast = fast.next;
if(fast == null){
return null;
}
k--;
}
while(fast.next!=null){
fast = fast.next;
slow = slow.next;
}
return slow;
}
5. 将两个有序链表合并为一个新的有序链表并返回,新链表是通过拼接给定的两个链表的所有节点组成的
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if(list1 == null && list2 == null){
return null;
}
ListNode newNode = new ListNode(-1);
ListNode tmp = newNode;
while(list1 != null && list2 != null){
if(list1.val < list2.val){
tmp.next = list1;
tmp = tmp.next;
list1 = list1.next;
}else{
tmp.next = list2;
tmp = tmp.next;
list2 = list2.next;
}
}
if(list1 == null){
tmp.next = list2;
}
if(list2 == null){
tmp.next = list1;
}
return newNode.next;
}
6. 编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前
public ListNode partition(ListNode pHead, int x) {
if(pHead == null){
return null;
}
ListNode cur = pHead;
ListNode bs = null;
ListNode be = null;
ListNode as = null;
ListNode ae = null;
while(cur != null){
if(cur.val < x){
if(bs == null){
bs = cur;
be = cur;
}else{
be.next = cur;
be = be.next;
}
}else{
if(as == null){
as = cur;
ae = cur;
}else{
ae.next = cur;
ae = ae.next;
}
}
cur = cur.next;
}
if(bs == null){
return as;
}
be.next = as;
if(ae!=null){
ae.next = null;
}
return bs;
}
7. 链表的回文结构
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null){
return true;
}
ListNode fast = head;
ListNode slow = head;
while(fast != null&&fast.next != null){
fast = fast.next.next;
slow = slow.next;
}
ListNode cur = slow.next;
while(cur!=null){
ListNode curNext = cur.next;
cur.next = slow;
slow = cur;
cur = curNext;
}
while(head!=slow){
if(slow.val != head.val){
return false;
}
if(slow == head.next){
return true;
}
head = head.next;
slow =slow.next;
}
return true;
}
8. 输入两个链表,找出它们的第一个公共结点## 标题
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA == null || headB == null){
return null;
}
ListNode pl = headA;
ListNode ps = headB;
int lenA = 0;
int lenB = 0;
while(pl!=null){
lenA++;
pl = pl.next;
}
while(ps!=null){
lenB++;
ps = ps.next;
}
pl = headA;
ps = headB;
int len = lenA - lenB;
if(len<0){
pl = headB;
ps = headA;
len = lenB - lenA;
}
while(len!=0){
pl = pl.next;
len--;
}
while(pl != ps){
pl = pl.next;
ps = ps.next;
}
if(ps == null&&pl == null){
return null;
}
return pl;
}
9. 给定一个链表,判断链表中是否有环
public 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. 给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null
public ListNode detectCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
if(fast == slow){
break;
}
}
if(fast == null||fast.next == null){
return null;
}
fast = head;
while(fast != slow){
fast = fast.next;
slow = slow.next;
}
return fast;
}