1.删除链表中等于给定值 val 的所有节点
class ListNode{
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
public class Demo01 {
public ListNode removeElements(ListNode head,int val){
//1.链表为空的情况
if(head == null){
return null;
}
//2.处理非头结点
ListNode prev = head;
ListNode node = head.next;
while(node != null){
if(node.val == val){
prev.next = node.next;
node = prev.next;
}else{
prev = node;
node = node.next;
}
}
//3.处理头结点
if(head.val == val){
head = head.next;
}
return head;
}
2.反转一个单链表
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
//1.判定链表是否为空
if(head == null){
return null;
}
//2.只有一个元素时
if(head.next == null){
return head;
}
//3.处理一般情况
ListNode newHead = null;
ListNode cur = head;
ListNode prev = null;
while(cur != null){
ListNode next = cur.next;
if(next == null){
//此时已经指向最后一个节点了
newHead = cur;
}
cur.next = prev;
prev = cur;
cur = next;
}
return newHead;
}
}
3.给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点
public ListNode middleNode(ListNode head) {
ListNode cur = head;
int steps = size(head)/2;
for(int i = 0;i < steps;i++){
cur = cur.next;
}
return cur;
}
private int size(ListNode head){
int size = 0;
for(ListNode cur = head;cur != null;cur = cur.next){
size++;
}
return size;
}
4.输入一个链表,输出该链表中倒数第k个结点
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
int len = size(head);
if(head == null || k <= 0 || k > len){
return null;
}
int offset = len - k;
ListNode cur = head;
for(int i = 0;i < offset;i++){
cur = cur.next;
}
return cur;
}
private int size(ListNode head){
int size = 0;
for(ListNode cur = head;cur != null;cur = cur.next){
size++;
}
return size;
}
}
5.将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null){
return l2;
}
if(l2 == null){
return l1;
}
ListNode newList = new ListNode(0);
ListNode tail = newList;
while(l1 != null && l2 != null){
if(l1.val < l2.val){
tail.next = new ListNode(l1.val);
l1 = l1.next;
tail = tail.next;
}else{
tail.next = new ListNode(l2.val);
l2 = l2.next;
tail = tail.next;
}
}
if(l1 != null){
tail.next = l1;
}
if(l2 != null){
tail.next = l2;
}
return newList;
}
6.编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前
public ListNode partition(ListNode pHead, int x) {
if(pHead == null){
return null;
}
if(pHead.next == null){
return pHead;
}
ListNode smallHead = new ListNode(-1);
ListNode smallTail = smallHead;
ListNode bigHead = new ListNode(-1);
ListNode bigTail = bigHead;
ListNode cur = pHead;
while(cur != null){
if(cur.val < x){
smallTail.next = new ListNode(cur.val);
smallTail = smallTail.next;
cur = cur.next;
}else{
bigTail.next = new ListNode(cur.val);
bigTail = bigTail.next;
cur = cur.next;
}
}
smallTail.next = bigHead.next;
return smallHead.next;
}
7.在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。
public ListNode deleteDuplication(ListNode pHead){
if(pHead == null){
return null;
}
if(pHead.next == null){
return pHead;
}
ListNode newHead = new ListNode(0);
newHead.next = pHead;
ListNode prev = newHead;
ListNode node = pHead;
while(node != null){
if(node.next != null && node.val == node.next.val){
while(node.next != null && node.val == node.next.val){
node = node.next;
}
prev.next = node.next;
node = node.next;
}else{
prev = prev.next;
node = node.next;
}
}
return newHead.next;
}
8.判断链表是否为回文结构。
public int getLength(ListNode head){
int count = 0;
ListNode cur = head;
while(cur != null){
cur = cur.next;
count++;
}
return count;
}
public ListNode getMiddle(ListNode head){
int length = getLength(head);
int half = length / 2;
ListNode cur = head;
for(int i = 0;i < half;i++){
cur = cur.next;
}
return cur;
}
public ListNode reverseList(ListNode head){
ListNode pHead = null;
ListNode cur = head;
while(cur != null){
ListNode next = cur.next;
cur.next = pHead;
pHead = cur;
cur = next;
}
return pHead;
}
public boolean chkPalindrome(ListNode A) {
ListNode middle = getMiddle(A);
ListNode rHead = reverseList(middle);
ListNode c1 = A;
ListNode c2 = rHead;
while(c1 != null && c2 != null){
if(c1.val != c2.val){
return false;
}
c1 = c1.next;
c2 = c2.next;
}
return true;
}
- 输入两个链表,找出它们的第一个公共结点
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA == null || headB == null){
return null;
}
int lenA = size(headA);
int lenB = size(headB);
if(lenA > lenB){
for(int i = 0;i < lenA - lenB;i++){
headA = headA.next;
}
}else if(lenB > lenA){
for(int i = 0;i < lenB - lenA;i++){
headB = headB.next;
}
}
while(headB != null){
if(headB == headA){
return headB;
}
headA = headA.next;
headB = headB.next;
}
return null;
}
private int size(ListNode head){
int size = 0;
for(ListNode cur = head;cur != null;cur = cur.next){
size++;
}
return size;
}
10.给定一个链表,判断链表中是否有环。
public boolean hasCycle(ListNode head) {
if(head == 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;
}
11.给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null
public ListNode detectCycle(ListNode head) {
//从链表头部出发,到入口点的距离
//和从快慢指针的交汇处出发到入口点的距离相等
if(head == null){
return null;
}
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和slow就已经重合了
ListNode cur1 = head;
ListNode cur2 = fast;
while(cur1 != cur2){
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
}