标签:java 有序 isp 回文 sys ++ cat element 从尾到头打印链表
Leetcode 简单链表题目
题目:将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode();
ListNode dummy = new ListNode();
dummy = head;
while(l1 != null && l2 != null){
if(l1.val > l2.val){
head.next =l2;
l2=l2.next;
head = head.next;
}else{
head.next = l1;
l1=l1.next;
head = head.next;
}
}
while(l1!=null){
head.next = l1;
l1=l1.next;
head=head.next;
}
while(l2!=null){
head.next = l2;
l2=l2.next;
head=head.next;
}
return dummy.next;
}
}
题目:给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null) return head;
ListNode pre = head;
ListNode cur = head.next;
while(cur!=null){
while(cur!=null && cur.val == pre.val) cur = cur.next;
pre.next = cur;
pre = pre.next;
}
return head;
}
}
题目:给定一个链表,判断链表中是否有环。
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
if(head == null || head.next == null) return false;
ListNode fast = head;
ListNode slow = head;
while(fast!=null && fast.next !=null && slow!=null){
// 一定要把if(fast == slow) return 放在移动过指针的下面,不然的话初始状态肯定是相等的
slow = slow.next;
fast = fast.next.next;
if(fast == slow) return true;
}
return false;
}
}
题目:编写一个程序,找到两个单链表相交的起始节点。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
int l1 = 0;
int l2 = 0;
ListNode p = headA;
ListNode q = headB;
while(p!=null){
l1++;
p = p.next;
}
while(q!=null){
l2++;
q = q.next;
}
p = headA;
q = headB;
int m = l1 - l2;
System.out.println(m);
if(m<0){
m=-m;
while(m-->0) q=q.next;
}else{
while(m-->0) p= p.next;
}
while(p!=null && q!=null){
if(p == q) return p;
p = p .next;
q = q.next;
}
return null;
}
}
题目:删除链表中等于给定值 val 的所有节点。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head == null) return head;
ListNode dummy = new ListNode(0);
ListNode h = dummy;
ListNode pre = head;
while(pre!=null){
while(pre != null && pre.val == val) pre = pre.next;
h.next = pre;
if(pre!=null) pre = pre.next;
h=h.next;
}
return dummy.next;
}
}
题目 : 反转一个单链表。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null) return head;
ListNode dummy = new ListNode(0);
ListNode pre = head;
ListNode cur = head.next;
dummy.next = null;
while(pre != null){
pre.next = dummy.next;
dummy.next = pre;
pre = cur;
if(cur != null)cur=cur.next;
}
return dummy.next;
}
}
题目:请判断一个链表是否为回文链表。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null) return true;
ListNode pre = head;
ListNode cur = head;
int len = 0;
while(pre != null){
pre=pre.next;
len++;
}
pre = head;
int mid = (len%2)==1?(len/2+2):(len/2+1);
while(mid-- >1) cur = cur.next;
cur = reverse(cur);
while(cur != null && pre != null){
if(pre.val != cur.val) return false;
pre = pre.next;
cur = cur.next;
}
return true;
}
// 将链表翻转,并返回翻转之后的头结点
public ListNode reverse(ListNode root){
if(root == null || root.next == null) return root;
ListNode dummy = new ListNode(0);
dummy.next = null;
ListNode pre = root;
ListNode cur = root.next;
while(pre != null){
pre.next = dummy.next;
dummy.next = pre;
pre = cur;
if(cur!=null) cur = cur.next;
}
return dummy.next;
}
}
题目:请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点。传入函数的唯一参数为 要被删除的节点 。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void deleteNode(ListNode node) {
node.val = node.next.val;
node.next = node.next.next;
}
}
题目: 给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode middleNode(ListNode head) {
if(head == null || head.next == null) return head;
ListNode p = head;
int len = 0;
int half = 0;
while(p!=null){
len++;
p=p.next;
}
half = len/2+1;
p=head;
while(half>1){
p=p.next;
half--;
}
return p;
}
}
题目:给你一个单链表的引用结点 head。链表中每个结点的值不是 0 就是 1。已知此链表是一个整数数字的二进制表示形式。请你返回该链表所表示数字的 十进制值 。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int getDecimalValue(ListNode head) {
int flag = 0;
int res = 0;
int len = 0;
ListNode pre = head;
while(pre!=null){
len++;
pre=pre.next;
}
pre = head;
while(pre!=null){
if(pre.val == 1) res+=Math.pow(2,len-flag-1);
flag++;
pre=pre.next;
}
return res;
}
}
题目:输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] reversePrint(ListNode head) {
int len = 0;
ListNode pre = head;
while(pre != null){
len++;
pre=pre.next;
}
pre = head;
int[] res = new int[len];
int index = len-1;
while(pre != null){
res[index] = pre.val;
pre = pre.next;
index--;
}
return res;
}
}
同上
题目:输入一个链表,输出该链表中倒数第k个节点。为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点。例如,一个链表有6个节点,从头节点开始,它们的值依次是1、2、3、4、5、6。这个链表的倒数第3个节点是值为4的节点。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
int len = 0;
ListNode pre =head;
while(pre != null){
pre = pre.next;
len++;
}
ListNode slow = head;
ListNode fast = head;
while(k>0){
fast = fast.next;
k--;
}
while(fast!=null){
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
同上
同上
同上
同上
题目 实现一种算法,删除单向链表中间的某个节点(即不是第一个或最后一个节点),假定你只能访问该节点。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void deleteNode(ListNode node) {
node.val = node.next.val;
node.next = node.next.next;
}
}
同上
同上
标签:java 有序 isp 回文 sys ++ cat element 从尾到头打印链表
原文地址:https://www.cnblogs.com/botak/p/13933271.html