在数据结构中链表成了我们常见的一种形式,链表的内容学习起来比较绕,学者容易和C语言中的数组或者指针进行混淆,下面列出几道关于链表的习题供大家参考。
编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前 。
源代码
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Partition {
public ListNode partition(ListNode pHead, int x) {
// < x
ListNode less = null;
ListNode lessLast = null;
// >= x
ListNode great = null;
ListNode greatLast = null;
ListNode cur = pHead;
while (cur != null) {
if (cur.val < x) {
if (less == null) {
less = cur;
} else {
lessLast.next = cur;
}
lessLast = cur;
} else {
if (great == null) {
great = cur;
} else {
greatLast.next = cur;
}
greatLast = cur;
}
cur = cur.next;
}
if (less == null) {
return great;
} else {
lessLast.next = great;
if (greatLast != null) {
greatLast.next = null;
}
return less;
}
}
}
2.
给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个
中间结点。
class Solution {
private int getLength(ListNode head) {
int len = 0;
for (ListNode cur = head; cur != null; cur = cur.next) {
len++;
}
return len;
}
public ListNode middleNode(ListNode head) {
int len = getLength(head);
int midLen = len / 2;
ListNode node = head;
for (int i = 0; i < midLen; i++) {
node = node.next;
}
return node;
}
}
class Solution {
public ListNode middleNode(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while (fast != null) {
fast = fast.next;
if (fast == null) {
break;
}
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
3.输入一个链表,输出该链表中倒数第k个结点。
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
int len = 0;
for (ListNode c = head; c != null; c = c.next) {
len++;
}
if (len < k) {
return null;
}
int steps = len - k;
ListNode r = head;
for (int i = 0; i < steps; i++) {
r = r.next;
}
return r;
}
}
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
ListNode front = head;
ListNode back = head;
for (int i = 0; i < k; i++) {
if (front == null) {
return null;
}
front = front.next;
}
while (front != null) {
back = back.next;
front = front.next;
}
return back;
}
}
4.把两个链表进行合并,并让他们进行有序排列。
public class PalindromeList {
public ListNode getMid(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while (fast != null) {
fast = fast.next;
if (fast == null) {
break;
}
fast = fast.next;
slow = slow.next;
}
return slow;
}
public ListNode reverse(ListNode head) {
ListNode result = null;
ListNode cur = head;
while (cur != null) {
ListNode next = cur.next;
cur.next = result;
result = cur;
cur = next;
}
return result;
}
public boolean chkPalindrome(ListNode A) {
ListNode mid = getMid(A);
ListNode h2 = reverse(mid);
ListNode n1 = A;
ListNode n2 = h2;
while (n1 != null && n2 != null) {
if (n1.val != n2.val) {
return false;
}
n1 = n1.next;
n2 = n2.next;
}
return true;
}
}
5.删除指定的所有结点
public class Solution {
public ListNode deleteDuplication(ListNode pHead)
{
if (pHead == null) {
return null;
}
ListNode prev = null;
ListNode p1 = pHead;
ListNode p2 = pHead.next;
while (p2 != null) {
if (p1.val != p2.val) {
prev = p1;
p1 = p2;
p2 = p2.next;
} else {
while (p2 != null && p2.val == p1.val) {
p2 = p2.next;
}
if (prev == null) {
pHead = p2;
} else {
prev.next = p2;
}
p1 = p2;
if (p2 != null) {
p2 = p2.next;
}
}
}
return pHead;
}
}
希望会对大家有帮助,有什么错误问题,还望大家指正。