本文介绍了交换单链列表的最后两个节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
设置T * = B(将B存储在某个地方) 设置A.next = C 设置T * .next = C.next(这可以从仅在列表末尾进行操作而得到概括) 设置C.next = T *
如何交换链表的最后两个节点?我正在尝试使用一个辅助节点,因为我认为有必要避免在此过程中丢失"一个节点...
How can I swap the last two nodes of a linked list? I'm trying to use a helper node as I think it's needed to avoid 'losing' a node in the process...
... Node node3 = new Node("Hi", null) ; Node node4 = new Node("Hello", null) ; ... // swap node3 & node4 Node temp = node3.succ ; node3.succ = null ; // this should be the last node now, so i set its pointer to null node2.succ = temp ; // the second's node successor becomes what used to be the last node temp = node4 ; // not sure how to use temp here. what should it point to if at anything?我认为我做错了,有什么提示吗?
I think I'm doing this wrong, any hints?
推荐答案假设您有一个链表 A->B->C ,并且您想交换 B 和 C :
Suppose you have a linked list A -> B -> C, and you want to swap B and C:
更多推荐
交换单链列表的最后两个节点
发布评论