Linked List Start!
(1)Delete Node in a Linked List

题意简单明了,用后一个节点来替换要删除的节点即可。代码如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public void deleteNode(ListNode node) {
node.val = node.next.val;
node.next = node.next.next;
}
}
(2)Reverse Linked List

解题方法一:递归法
在反转当前节点之前先反转后续节点。这样从头结点开始,层层深入直到尾结点才开始反转指针域的指向。简单的说就是从尾结点开始,逆向反转各个结点的指针域指向。
代码如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
if (head == null) {
return null;
}
if (head.next == null) {
return head;
}//递归解法一定要有判断条件:此时说明到达反转前的尾节点,可以开始执行反转操作
ListNode p = head.next;
ListNode n = reverseList(p);
p.next = head; //当前节点的指针域指向前一节点
head.next = null; //前一节点的指针域置null
return n;
}
}
解题方法二:迭代法
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null;// 第一个节点肯定是(反转后的)尾节点,尾节点的next为NULL
while (head != null) {//当前节点为空,说明到了(反转前的)尾节点
ListNode temp = head.next;//当前节点的下一个指针临时储存
head.next = pre;//当前节点指针指向上一个节点
//指针向下移动
pre = head;
head = temp;
}
return pre;
}
}
(3)Remove Duplicates from Sorted List

解题思路:遍历一次,遇到重复的节点删除(将前一节点的指针指向该节点的下一个节点)即可。
代码如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null) {
return null;
}
ListNode node = head;
while (node.next != null) {
if (node.val == node.next.val) {
node.next = node.next.next;
} else {
node = node.next;
}
}
return head;
}
}
(4)Merge Two Sorted Lists

解题思路:简单明了,创建一个新节点dummy作为头结点,然后l1和l2进行比较,小的先加入,哪个链表有剩余最后加入即可。
代码如下:
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode lastNode = dummy; while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
lastNode.next = l1;
l1 = l1.next;
} else {
lastNode.next = l2;
l2 = l2.next;
}
lastNode = lastNode.next;
} if (l1 != null) {
lastNode.next = l1;
} else {
lastNode.next = l2;
}
return dummy.next;
}
}
(5)Swap Nodes in Pairs

解题思路:n1、n2为第一组结点,头结点的next指向n2,n1的next指向n2的next,n2的next指向n1。然后进行下一组...
代码如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;//新节点的next指针指向原链表的头结点 head = dummy;//现在的头结点就是dummy
while (head.next != null && head.next.next != null) {
ListNode n1 = head.next;
ListNode n2 = head.next.next;
// head->n1->n2->...
// => head->n2->n1->...
head.next = n2;
n1.next = n2.next;
n2.next = n1;
//移向下一组节点
head = n1;
}
return dummy.next;
}
}
Linked List Start!的更多相关文章
- [LeetCode] Linked List Random Node 链表随机节点
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
- [LeetCode] Plus One Linked List 链表加一运算
Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...
- [LeetCode] Odd Even Linked List 奇偶链表
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- [LeetCode] Delete Node in a Linked List 删除链表的节点
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- [LeetCode] Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- [LeetCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- [LeetCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode] Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
随机推荐
- 写给java开发的运维笔记
转载至:https://github.com/biezhi/java-bible/tree/master/learn_server
- HUD 4007 [扫描线][序]
/* 大连热身B题 不要低头,不要放弃,不要气馁,不要慌张 题意: 坐标平面内给很多个点,放置一个边长为r的与坐标轴平行的正方形,问最多有多少个点在正方形内部. 思路: 按照x先排序,然后确定x在合法 ...
- Python基础篇【第7篇】: 面向对象(1)
面向对象技术简介 相近对象,归为类 在人类认知中,会根据属性相近把东西归类,并且给类别命名.比如说,鸟类的共同属性是有羽毛,通过产卵生育后代.任何一只特别的鸟都在鸟类的原型基础上的.面向对象就是模拟了 ...
- ThrottleAttribute
/// <summary> /// Decorates any MVC route that needs to have client requests limited by time. ...
- UIPopoverController使用
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 解决java文件编码和windows7系统(中文版)默认编码冲突所导致的乱码情况
开篇从一个比较简单但是也比较蛋疼的问题开始吧. 背景介绍:我是新手小白,初学java. 问题介绍:在使用UTF-8编码格式写java文件时,编译出现问题. 原因分析:1.java文件的编码格式是UTF ...
- Linux 脚本命令结果输出到文件
From: http://bbs.chinaunix.net/thread-1997207-1-1.html sh test.sh | tee log.txt
- sencha touch+phonegap+node.js打包
这讲我们来讲解下如何使用phonegapa创建项目环境并通过她们将sencha touch打包成app,这里我们只讲解打包android的apk,打包ios的过程有点类似,但是需要在mac环境下,最后 ...
- jQuery检测滚动条(scroll)是否到达底部
一.jQuery检测浏览器window滚动条到达底部 jQuery获取位置和尺寸相关函数: $(document).height() 获取整个页面的高度 $(window).height() ...
- socket 网络编程
1. 基础socket库 socket.h: /** * 网络套接字库 */ #ifndef Socket_h #define Socket_h #include <stdio.h> #i ...