题目: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -&…
[链表] Q:Write code to remove duplicates from an unsorted linked list FOLLOW UP How would you solve this problem if a temporary buffer is not allowed? 题目:编码实现从无序链表中移除重复项. 如果不能使用临时缓存,你怎么编码实现? 解答: 方法一:不使用额外的存储空间,直接在原始链表上进行操作.首先用一个指针指向链…
题目描述 输入一个链表,输出该链表中倒数第k个结点. 解法 pre 指针走 k-1 步.之后 cur 指针指向 phead,然后两个指针同时走,直至 pre 指针到达尾结点. 即cur与pre始终相距k-1. 当用一个指针遍历链表不能解决问题的时候,可以尝试用两个指针来遍历链表.可以让其中一个指针遍历的速度快一些. 此题需要考虑一些特殊情况.比如 k 的值小于 0 或者大于链表长度. package demo; public class Solution { public static clas…
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3826 访问. 删除链表中等于给定值 val 的所有节点. 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3->4->5 Remove all elements from a linked list of integers that have value val. Input…
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 这个求单链表中的环的起始点是之前那个判断单链表中是否有环的延伸,可参见我之前的一篇文章 (http://www.cnblogs.com/grandyang/p/4137187.html). 还是要设…
输入一个链表,输出该链表中倒数第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) { ListNode p = head; ListNode pre = head; int a =k; int count…