删除链表中的重复节点
题目描述
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
public class Solution {
public ListNode deleteDuplication(ListNode pHead) {
ListNode pre=new ListNode(-1);
ListNode cur=pHead;
ListNode re=pre;
while(cur!=null){
if(cur.next!=null&&cur.val==cur.next.val){
while(cur.next!=null&&cur.val==cur.next.val){
cur=cur.next;
}
}
else {
re.next=new ListNode(cur.val);
re=re.next;
}
cur=cur.next;
}
return pre.next;
}
}
链表分割
题目描述
现有一链表的头指针 ListNode* pHead,给一定值x,编写一段代码将所有小于x的结点排在其余结点之前,且不能改变原来的数据顺序,返回重新排列后的链表的头指针。
public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
if(pHead == null){
return null;
}
ListNode cur=pHead;
ListNode min=new ListNode(-1);
ListNode max=new ListNode(-1);
ListNode min1=min;
ListNode max1=max;
while(cur!=null){
if(cur.val<x){
min.next=cur;
min=min.next;
}
else {
max.next=cur;
max=max.next;
}
cur=cur.next;
}
min.next=max1.next;
max.next=null;
return min1.next;
}
}
相交链表
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode A = headA, B = headB;
while (A != B) {
A = A != null ? A.next : headB;
B = B != null ? B.next : headA;
}
return A;
}
}
链表的回文结构
题目描述
对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构。
给定一个链表的头指针A,请返回一个bool值,代表其是否为回文结构。保证链表长度小于等于900。
测试样例:
public class PalindromeList {
public boolean chkPalindrome(ListNode A) {
Stack<Integer> stack=new Stack<>();
ListNode a=A;
while(A!=null){
stack.push(A.val);
A=A.next;
}
while (!stack.empty()){
if(stack.pop()!=a.val){
return false;
}
else a=a.next;
}
return true;
}
}
合并两个有序链表
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head=new ListNode(-1);
ListNode prev=head;
while(l1!=null&&l2!=null){
if(l1.val<l2.val){
prev.next=l1;
l1=l1.next;
}
else{
prev.next=l2;
l2=l2.next;
}
prev=prev.next;
}
if(l1!=null){
prev.next=l1;
}
else{
prev.next=l2;
}
return head.next;
}
}
链表中倒数第K个节点
题目描述
输入一个链表,输出该链表中倒数第k个结点。
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(head == null || k ==0 ){
return null;
}
ListNode slow=head;
ListNode fast=head;
for(int i=0;i<k;i++){
if(fast==null){
return null;
}
fast=fast.next;
}
while(fast!=null){
slow=slow.next;
fast=fast.next;
}
return slow;
}
}
链表的中间结点
给定一个头结点为 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;
}
}
反转链表
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev=null;
ListNode cur=head;
while(cur!=null){
ListNode next=cur.next;
cur.next=prev;
prev=cur;
cur=next;
}
return prev;
}
}
移除链表元素
给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode cur=head;
while(head!=null&&head.val==val){
head=head.next;
}
if(head==null){
return null;
}
while(cur.next!=null){
if(cur.next.val==val){
cur.next=cur.next.next;
}
else{cur=cur.next;}
}
return head;
}
}