我们接着上一篇学的知识进行继续,将有关例题一一讲解,大家点击链接跳转到原题出处,感谢各位的支持。
class Solution {
public ListNode removeElements(ListNode head, int val) {
if (head == null) {
return head;
}
head.next = removeElements(head.next, val);
return head.val == val ? head.next : head;
}
}
2.反转一个单链表。
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode newHead = reverseList(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
}
3. 给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。
class Solution {
public ListNode middleNode(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
}
。
4. 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
} else if (l2 == null) {
return l1;
} else if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else {
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
}
5. 编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前 。
public class Partition {
public ListNode partition(ListNode pHead, int val) {
if(pHead == null) return null;
Stack<Integer> stack = new Stack<>();
ListNode cur = pHead;
ListNode prev = null;
while(cur!=null) {
if(cur.val < val) {
stack.add(cur.val);
if(cur == pHead) {
pHead = pHead.next;
cur = pHead;
}else {
cur = cur.next;
prev.next = cur;
}
}else {
prev = cur;
cur = cur.next;
}
}
while(!stack.isEmpty()) {
ListNode newNode = new ListNode(stack.pop());
newNode.next = pHead;
pHead = newNode;
}
return pHead;
}
}
6.链表的回文结构。
public class PalindromeList {
public boolean chkPalindrome(ListNode A) {
if(A==null){
return true;
}
// write code here
//找到中点
ListNode fast=A;
ListNode slow=fast;
ListNode cur=fast;
while(fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
}
//翻转
cur=slow.next;
while(cur!=null){
ListNode curNext=cur.next;
cur.next=slow;
slow=cur;
cur=curNext;
}
while(A!=slow){
if(A.val!=slow.val){
return false;
}
if(A.next!=slow){
return true;
}
A=A.next;
slow=slow.next;
}
return true;
}
}
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
}
ListNode pA = headA, pB = headB;
while (pA != pB) {
pA = pA == null ? headB : pA.next;
pB = pB == null ? headA : pB.next;
}
return pA;
}
}
public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null || head.next == null) {
return false;
}
ListNode slow = head;
ListNode fast = head.next;
while (slow != fast) {
if (fast == null || fast.next == null) {
return false;
}
slow = slow.next;
fast = fast.next.next;
}
return true;
}
}
9. 给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 NULL
public class Solution {
public ListNode detectCycle(ListNode head) {
if (head == null) {
return null;
}
ListNode slow = head, fast = head;
while (fast != null) {
slow = slow.next;
if (fast.next != null) {
fast = fast.next.next;
} else {
return null;
}
if (fast == slow) {
ListNode ptr = head;
while (ptr != slow) {
ptr = ptr.next;
slow = slow.next;
}
return ptr;
}
}
return null;
}
}
这些是比较出名的面试题,大家参考学习,这里例子不多,剩下的大家自行获取。