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 ...
随机推荐
- 转 makefile
转自http://blog.chinaunix.net/uid-21411227-id-1826747.html gcc是gnu旗舰产品,目前基本上就是和unix捆绑在一起分发的.这个东西功能强大,但 ...
- ssh 服务器之间公钥认证方式的配置
前言 项目中需要编写脚本在服务器之间上传或者下载文件,但没有相关服务器来测试脚本,于是就着手安装两台server,然后用ssh的相关命令去配置server之间公钥认证登录. 步骤 1. 在VM Box ...
- xcode:关于Other Linker Flags
一.关于Other Linker Flags xcode中,在“Targets”选项下有Other Linker Flags选项,在这里可以填写xcode链接器的参数,如:-ObjC.-all_loa ...
- 如何让CCLayer创造的地图,左右滑动不出现黑边
在都是scale为1的情况下,效果图如下: , 绿色的是Screen,它的大小和坐标不变,可以理解为CCScene,下面两个就是用CCLayer的地图,有2个Layer,深颜色的就是和Scrren一样 ...
- 浅谈oracle10G spfile与pfile(转)
转自:http://blog.csdn.net/onebigday/article/details/6108348,http://www.linuxidc.com/Linux/2012-11/7371 ...
- AX7: Get started developing
This blog will show how you can start making a customization in AX 7 by showing you the steps needed ...
- loaded the "ViewController" nib but the view outlet was not set.'
错误代码: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[U ...
- Javascript中的Promise
Promise定义 Promise是CommonJs的规范之一,包含resolve,reject,done,fail,then等方法,能够帮助我们控制代码的流程,避免函数的多层嵌套.异步在web开发中 ...
- According to TLD or attribute directive in tag file, attribute test does not accept any expressions
HTTP Status 500 - /WEB-INF/views/emp/list.jsp (line: 30, column: 4) According to TLD or attribute di ...
- The source attachment does not contain the source for the file ActionSupport.class 错误
报错 Syntax error, insert ";" to complete FieldDeclaration 报错 The source attachment does ...