6.从尾到头打印链表
题目描述:输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
Stack<Integer> stack=new Stack<>();
ListNode node=listNode;
while(node!=null){
stack.push(node.val);
node=node.next;
}
ArrayList<Integer> arr=new ArrayList<>();
while(stack.size()!=0){
arr.add(stack.pop());
}
return arr;
}
}
18.删除链表的节点
题目描述:在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5。
思路:先新建一个结点,指向pHead,防止头节点被删掉,如果相等的话往后面重复查找,不等的话就指向下一个。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ListNode deleteDuplication(ListNode pHead)
{
if(pHead==null||pHead.next==null){
return pHead;
}else{
//新建一个头节点,防止头节点要被删掉
ListNode head=new ListNode(-1);
head.next=pHead;
ListNode pre=head;
ListNode p=pHead;
ListNode next=null;
while(p!=null&&p.next!=null){
next=p.next;
if(p.val==next.val){ //如果p的val和next的val相等的话,那就往后重复查找
while(next!=null&&p.val==next.val){
next=next.next; //向后重复找
}
pre.next=next;
p=next;
}else{ //如果当前结点和下一个节点值不想等,那么就往后移动
pre=p;
p=p.next;
}
}
return head.next;
}
}
}
22.链表中倒数第k个节点
题目描述:输入一个链表,输出该链表中倒数第k个结点。
思路:暴力思路:先遍历一次得到链表的长度,然后再遍历到n-k+1个就是倒数第k个;
比较好的思路:通过指针,因为要找到倒数第k个,定义两个指针,first先走到k-1位置;然后从第k步开始,second指针从1开始走,当first走到末尾,second也走到了倒数第k个位置。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(head==null||k==0){
return null;
}
ListNode first=head;
ListNode second=head;
int step=1;
for(int i=0;i<k-1;i++){
if(first.next!=null){
first=first.next;
}else{
return null;
}
}
while(first.next!=null){
first=first.next;
second=second.next;
}
return second;
}
}
42.反转链表
题目描述:输入一个链表,反转链表后,输出新链表的表头。
思路:如果head==null或者head.next==null,那么返回head。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head==null||head.next==null){
return head;
}
//ListNode p=head;
ListNode reversehead=null;
ListNode next=null;
while(head!=null){
next=head.next;
head.next=reversehead;
reversehead=head;
head=next;
}
return reversehead;
}
}
25.合并两个排序的链表
题目描述:输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
if(list1==null&&list2!=null){
return list2;
}else if(list2==null&&list1!=null){
return list1;
}else if(list1==null&&list2==null){
return null;
}
ListNode mergehead=null;
if(list1.val<=list2.val){
mergehead=list1;
list1=list1.next;
mergehead.next=Merge(list1,list2);
}else{
mergehead=list2;
list2=list2.next;
mergehead.next=Merge(list1,list2);
}
return mergehead;
}
}