1.编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前
public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
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;
}
}
2.在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针
public class Solution {
public ListNode deleteDuplication(ListNode pHead) {
if(pHead == null) {
return null;
}
if(pHead.next == null) {
return pHead;
}
ListNode newHead = new ListNode(1);
newHead.next = pHead;
ListNode prev = newHead;
ListNode node = prev.next;
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;
}
}
3.判定链表的回文结构
public class PalindromeList {
public boolean chkPalindrome(ListNode A) {
ListNode B = reverseList(A);
while(A != null) {
if(A.val != B.val) {
return false;
}
A = A.next;
B = B.next;
}
return true;
}
public ListNode reverseList(ListNode head) {
if(head == null) {
return null;
}
if(head.next == null) {
return head;
}
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;
}
}
4.输入两个链表,找出它们的第一个公共结点
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
int lenA = size(headA);
int lenB = size(headB);
if(lenA > lenB) {
int offset = lenA-lenB;
for(int i = 0; i < offset;i++) {
headA = headA.next;
}
}else {
int offset = lenB - lenA;
for(int i = 0;i < offset;i++) {
headB = headB.next;
}
}
while(headA != null && headB != null) {
if(headA == headB) {
return headA;
}
headA = headA.next;
headB = headB.next;
}
return null;
}
public int size(ListNode head) {
int size = 0;
for(ListNode cur = head;cur != null; cur = cur.next) {
size++;
}
return size;
}
}
5.给定一个链表,判断链表中是否有环
public class Solution {
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;
}
}
6.给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null
public class Solution {
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;
}
ListNode cur1 = head;
ListNode cur2 = fast;
while(cur1 != cur2) {
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
}
}